I’m importing from a JSON file and I get duplicates every time the importer runs.
The foreach
$name = $data->name;
$desc = $data->description;
$type = $data->type;
$tutor = $data->tutor;
$location = $data->location;
$planned_start = $data->planned_start;
$actual_start = $data->actual_start;
$actual_end = $data->actual_end;
$max_attendees = $data->max_attendees;
The importer
$check_title = get_page_by_title( $name);
$my_post = array(
'ID' => $check_title->ID,
'post_title' => wp_strip_all_tags($name),
'post_type' => 'training',
'post_content' => $desc,
'post_status' => 'publish',
'post_author' => 1,
'comment_status' => 'closed',
'ping_status' => 'closed',
'meta_input' => array(
'course_type' => $type,
'tutor' => $tutor,
'location' => $location,
'planned_start' => $planned_start,
'actual_start' => $actual_start,
'actual_end' => $actual_end,
'max_attendees' => $max_attendees,
),
'tax_input' => array(
'custom_cat_training' => $type,
),
);
if (empty($check_title)){
$post_id = wp_insert_post($my_post);
wp_set_object_terms($post_id, $type, 'custom_cat_training');
} else {
$post_id = wp_update_post($my_post);
wp_set_object_terms($check_title->ID, $type, 'custom_cat_training');
}
I’m probably missing something obvious but I can’t for the life of me work it out.
2
Answers
I used get_page_by_path($trimmed, OBJECT, 'training');
and used the page slug rather than title
you can check it with it
https://developer.wordpress.org/reference/functions/post_exists/