【发布时间】:2013-12-20 18:04:41
【问题描述】:
我有一个使用自定义元框的站点,该站点使用 Meta-Box 插件包含以下字段。代码如下 `
$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' );'函数来检查所有以前的 CPT 属性的 MLS 编号,以确保它之前没有被输入过。我使用的代码是:
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 中,当我尝试发布时屏幕变白。调试模式也不提供任何帮助。 :/
我确定我在某处犯了一些编程重大错误。有人可以指出吗?或者也许指出我正确的方向?或提出完全不同的建议?
谢谢 基思
【问题讨论】:
标签: php wordpress metadata custom-post-type