WordPress – Sync wordpress post with JSON feed

The important step is to assign a unique ID to every item you get from the source. Hopefully the JSON feed already includes one, but otherwise you will have to somehow create one yourself based on the JSON content.

Once you have the value, you store it as a meta value of the post being created. The code should look something like this

$json = get_json_item();
$uuid = get_json_uuid($json);
// check if exist based on the json_uuid meta
$t = get_posts(array('meta_key' => 'json_uuid', 'meta_value' => $uuid));
if (count($t) == 0) { // create new post
  $pid = wp_insert_post(values extracted from $json);
  update_post_meta($pid,'json_uuid',$uuid);
} else { // post exist
  wp_insert_post(array('ID' => $p[0]->ID,values extracted from $json));
}