在我的插件中,我有一个带有几个输入字段( customer_name 和 customer_email )的表单,该表单在提交时会创建一个帖子。
提交后,我还要检查数据库中是否已经存在具有相同 post_title 的帖子:
// posts
$customer_name = sanitize_text_field($_POST['customer_name']);
// post_meta
$customer_email = sanitize_text_field($_POST['customer_email']);
if( post_exists($customer_name) ) {
// the post exists, update the post
} else {
// the post does not exist, add new post
}
..效果很好。
但是我要做的不仅是检查 customer_name 是否已经存在,还要检查 customer_name 是否已经具有 customer_email (在post_meta中)与提交的相同。
这样做,我将知道是否需要更新现有帖子或创建新帖子。
答案 0 :(得分:1)
$post_id = post_exists($customer_name)
if($post_id){
if ( metadata_exists( 'post', $post_id, 'customer_email' ) ) {
// Customer email exist
$post_customer_email = get_post_meta( $post_id, 'customer_email', true );
if($post_customer_email == $customer_email){
echo "already exist"; // do your stuff here
}
}
}