У меня проблема при импорте сообщений в WordPress из API. Проблема в том, что иногда сообщение импортируется дважды, и я не могу понять, почему, потому что я проверяю, существует ли сообщение уже.
Все это работает с вышеуказанной функцией, которая вызывается с WP CRON
каждые 2 минуты
public function loadAllNews(){
//get Posts from API
$news = $getNews->news;
foreach ($news as $new) {
// Gather news data.
$data = array(
'post_title' => $new->Headline,
'post_excerpt' => $new->Teaser,
'post_content' => $new->BodyHTML, //TODO BodyHTMl verwenden, damit HTML Tags da sind
'post_date' => $new->ShowDate,
'meta_input' => array(
'myconvento_thumbnail_url' => $thumbnail_url,
'myconvento_thumbnail_url_big' => $thumbnail_url_big,
'myconvento_thumbnail_url_download' => $download_link,
'myconvento_subline' => $new->Subline,
'myconvento_media' => $media,
'myconvento_contacts' => $contacts,
'myconvento_id' => $new->Id,
'is_myconventoPost' => true,
),
'post_status' => 'publish',
'post_type' => 'pressemitteilung',
'post_parent' => 0,
);
//we must look if the post exist -> if the post exist then update else insert
$query_args = array(
'post_type' => 'pressemitteilung',
'meta_key' => 'myconvento_id',
'meta_value' => $new->Id
);
$query = new WP_Query($query_args);
//If the job doesnt exist -> then create new else update
$post_id = 0;
if (!$query->have_posts()) {
$post_id = wp_insert_post($data);
} else {
$query->the_post();
$post_id = get_the_ID();
$data['ID'] = $post_id;
wp_update_post($data);
}
}
}
Может быть, у кого-то есть хорошее представление об этом.