Check for duplicate custom field value before running wp_insert_post

That is, I want to prevent 'guid_review' ACF value being duplicate by checking it before creating the post.

foreach ($items as $itemKey = $item) {


            $myPost = array(
                'post_type' = 'client_review',
                'post_title' =(string) $item-title, //the object as a string before attempting to manipulate it.
                'post_content' =(string) $item-description                

            );


            $inserted_post_id = wp_insert_post( (array) $myPost );



            if( $inserted_post_id ){

                add_post_meta( (int) $inserted_post_id,'guid_review',(string)$item-guid);
                add_post_meta($inserted_post_id,'link_review',(string)$item-link);
                add_post_meta($inserted_post_id,'author_review',(string)$item-author);
                add_post_meta($inserted_post_id,'rating_review',(string)$item-children('d3', true)-rating);
                add_post_meta($inserted_post_id,'certified_review',(string)$item-children('d3', true)-certified);
                add_post_meta($inserted_post_id,'pub_date_review',(string)$item-pubDate);              

            }

            }

Topic advanced-custom-fields post-duplication wp-insert-post Wordpress

Category Web


You can run a WP_query using meta_key and meta_value args

Take a look here WP_User_Query

If the result is non - empty - you know you have a duplicate!

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.