Reputation: 524
In my plugin I have a form with a couple of input fields (customer_name and customer_email) which creates a post when submitted.
When submitted, I'm also checking to see if a post with the same post_title already exists in the database:
// 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
}
..which works fine.
But what I need to do is not only check if the customer_name already exists, but also check if that customer_name already has a customer_email (in post_meta) the same as the one that was submitted.
By doing this I will know if I need to update an existing post or create a new post.
Upvotes: 0
Views: 330
Reputation: 11861
$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
}
}
}
Upvotes: 1