dimanche 24 février 2019

If post exists and post_meta exists

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.

Aucun commentaire:

Enregistrer un commentaire