0

I have to synchronize an external JSON feed with wp post. this have to be a hourly chron job. I have managed out how to import, and update posts from yhe external json feed using wp_insert_post(), but how can I delete posts that are no more in the json?

This is the script I found for importing, or updating the posts:

//check if post with id $json_id is already in database, if so, update post $json_id
if (get_post_status($json_id)) {
    $post = array(
    'ID'                =>  $json_id,
    'post_title'        =>  $json_title,
    'post_content'      =>  $json_content,
    );  

    wp_insert_post($post);
}
//if not in database, add post with id $json_id
else {
    $post = array(
    'import_id'         =>  $json_id,
    'post_title'        =>  $json_title,
    'post_content'      =>  $json_content,
    );  

    wp_insert_post($post);
}
  • Does the JSON feed include any sort of unique identification for each post? If so, store that ID as post meta, and then you can delete posts whose meta ID is missing from the JSON feed. – Jacob Peattie Jun 17 at 13:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.