我有一個網站使用Meta-Box插件的以下字段的自定義元框。代碼如下 `檢查是否有發佈的自定義元數據的重複Wordpress帖子
$meta_boxes[] = array(
'title' => 'MLS ID',
'pages' => array('property'),
'fields' => array(
array(
'name' => 'MLS ID',
'id' => "IntegratorPropertyID",
'desc' => 'MLS: e.g. 240091025-217',
'type' => 'text',
),
array(
'name' => 'Test MLS',
'id' => "mlsTest",
'desc' => 'Test MLS for Duplicate',
'type' => 'button',
),
),
'validation' => array(
'rules' => array(
"IntegratorPropertyID" => array(
'required' => true
),
),
'messages' => array(
"IntegratorPropertyID" => array(
'required' => 'MLS is required',
),
)
)
);
現在什麼即時尋找是增加一個「add_action('save_post', 'checkMLS');
」功能來檢查MLS號以前所有的CPT財產,以確保它之前沒有被輸入。我使用的代碼是:
function checkMLS($post_id) {
$slug = 'property';
if ($slug != $_POST['post_type']) {
return;
}
$mls2 = rwmb_meta('IntegratorPropertyID', 'type=text', $post_id);
$args = array('post_type' => 'property');
$loop = new WP_Query($args);
while ($loop->have_posts()) : $loop->the_post();
$this1 = get_the_ID();
$mls1 = rwmb_meta('IntegratorPropertyID', 'type=text', $this1);
if ($mls2 == $mls1) {
$my_post = array(
'ID' => $post_id,
'IntegratorPropertyID' => 'DUPLICATE!'
);
wp_update_post($my_post);
return;
}
endwhile;
}
add_action('save_post', 'checkMLS');
該代碼在functions.php中找到,當我嘗試發佈屏幕變白。調試模式也不提供任何幫助。 :/
我敢肯定我正在編寫一些主要的錯誤。有人能指出嗎?或者讓我指向正確的方向?或者建議完全不同的東西?
感謝 基思
感謝這工作就像一個魅力:)對不起,我從來沒有回答 –