Wordpress Check for and remove duplicate posts before using wp-insert-post. Data decoded from Json wp-remote-get. I am inserting into a Custom Post type and the unique value is 'feefo_id'.

I have tried creating a if loop around the wp-insert but when I do that I get 1 post and not all 17.

$feefo_data = wp_remote_get( 'https://api.feefo.com/api/10/reviews/all?enter code heremerchant_identifier=hide-and-seek-travel' );

$feefo_data_decode = json_decode( $feefo_data['body'] );

var_dump($feefo_data_decode);


foreach ( $feefo_data_decode->reviews as $item ) {
    $feefo_id       =$item->service->id;
    $post_title    = $item->service->title; // post title
    $post_content  = $item->service->review;
    $feefo_display_names    =   $item->customer->display_name;
    $feefo_display_location =   $item->customer->display_location;
    $feefo_rating   =   $item->service->rating->rating;




        $my_post = array(
            'post_title'  => $post_title,
            'post_content' => $post_content,
            'post_status' => 'publish',
            'post_type'   => 'feefo_reviews',
    );


$post_id = wp_insert_post( $my_post );

update_field( 'feefo_display_names', $feefo_display_names, $post_id );
update_field( 'feefo_display_location', $feefo_display_location, $post_id );
update_field( 'feefo_rating', $feefo_rating, $post_id );
//feefo_id is unique
update_field( 'feefo_id', $feefo_id , $post_id );
}

Currently the code above pulls the data from feefo, and adds to to a custom post called 'feefo_reviews'. Thou when I pull again it duplicates.

I am looking to avoid inserting duplicates. The unique id is $feefo_id and is inserted into an ACF field called feefo_id

2 Answers

1
Sjors Roelofs On Best Solutions

A solution is to save the feefo_id as a meta value to the created post. At every new pull, you first check if there is already a post with that meta value. If true: modify the existing post, if not: create a new post.

0
Dave Reilly On

I seem to have resolved the problem but the logic eludes me. Starting and ending WP-Query code. If someone has some free time could you talk me through the logic of what is happening in the wp_query?

        $my_post = array(
            'post_title'  => $post_title,
            'post_content' => $post_content,
            'post_status' => 'publish',
            'post_type'   => 'feefo_reviews',
//Set feefo_id as meta to be called upon with wp_Query as setting it as ID did not work.
            'meta_input'  => array(
                'feefo_id' => $feefo_id
            )
    );


$mg_args = array(
    'post_type' => 'feefo_reviews',
    'meta_query' => array(
        array(
            'key' => 'feefo_id',
            'value' => $feefo_id,
        )
    )
        );

        $mg_query =new WP_Query( $mg_args );
            if( $mg_query ->have_posts()){

$post_id = wp_insert_post( $my_post );

update_field( 'feefo_display_names', $feefo_display_names, $post_id );
update_field( 'feefo_display_location', $feefo_display_location, $post_id );
update_field( 'feefo_rating', $feefo_rating, $post_id );
//feefo_id is unique
update_field( 'feefo_id', $feefo_id , $post_id );
}
}