0

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);              

            }

            }

1 Answer 1

1

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!

3
  • 1
    Please add more detail (code) to your answer as an explanation to what you mean May 4, 2014 at 11:56
  • Actually am trying to insert XML data into a custom post type called 'Reviews'.I have successfully inserted each of them as a post where each post would have a unique ID determined by a variable $inserted_post_id.But only the way I can prevent my XML information from duplicating while inserting is by filtering by an unique identifier on XML data which is 'guid'.So going back to my question,I want to check whether guid value is already inserted into database or not.I have set guid and other require field through ACF plugin.So where in the code above should I query the DB and how would that be?
    – suascat
    May 5, 2014 at 11:14
  • In above 'guid_review' is a custom field I have created to store the value that comes from '$item->guid' .
    – suascat
    May 5, 2014 at 11:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.