You can save AdvertID
with your post and use it to check if the job was imported before.
/**
* Gets a job by AdvertId.
*
* @param string $advert_id The AdvertId.
* @return WP_Post|bool The job or <false> if no job is found.
*/
function get_job_by_advert_id( $advert_id ) {
$args = array(
'post_type' => 'jobs',
'meta_query' => array(
'key' => 'advert_id',
'value' => $advert_id,
),
);
$posts = get_posts( $args );
if (empty($posts)) {
return false;
}
return $posts[0];
}
// Check if the job was imported before.
$job = get_job_by_advert_id( $item->AdvertId );
if ( $job ) {
// The job was imported before, update it.
$args = array(
'ID' => $job->ID,
'post_content' => $item->description,
'post_date' => $item_date,
'post_title' => $item_title,
);
$post_id = wp_update_post( $args );
} else {
// The job is new. Insert it.
$args = array(
'post_content' => $item->description,
'post_date' => $item_date,
'post_title' => $item_title,
'post_status' => 'publish',
'post_type' => 'jobs',
);
$post_id = wp_insert_post($args);
// Save the AdvertId for the next import.
add_post_meta( $post_id, 'advert_id', $item->AdvertId, true);
}