最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Import external API for custom post type issue, problem: duplicate post issue or more

programmeradmin4浏览0评论

I am importing an external API to a custom post type called “magazine”.

I am working with a mu-plugin. I can import my info, but my posts are duplicated.

When I reset the number of posts for testing it can go from 50 posts to 200 for example and I don't understand why. There are double posts

I retrieve in my ID in an ACF field And the title in a classic WordPress title function

Here is my code:


function interval_cron ($schedules){
    $schedules ['five_minutes'] = array(
        'interval' => 300,
        'display' => esc_html__('all five minutes'),
    );
    return $schedules;
}
add_filter ('cron_schedules', 'interval_cron');

if ( !wp_next_scheduled ('api_cron') ):
    wp_schedule_event ( time(), 'five_minutes', 'api_cron' );
endif;

add_action ('api_cron', 'my_api');

function my_api(){ 


    if ( defined ('DOING_AUTOSAVE') and DOING_AUTOSAVE ){
        return;
    }


    if ( wp_is_post_revision ( get_the_id() ) ){
        return;
    }

    $request = wp_remote_get ('my url');

    if ( is_wp_error ($request) ){
        return false;
    } 

    $body = wp_remote_retrieve_body ($request);

    $data = json_decode ($body, true); ?>

    <?php foreach ($data as $data_info):

    $title = $data_info ['titre'];
    $id_catalog = $data_info ['revueid'];

    $arg = get_posts ( array (
       'post_type' => 'magazine',
       'meta_key'=> 'id_magazine',
       'meta_value' => $id_catalog,
       'posts_per_page' => 1,
     ));

    $post_catalog = wp_insert_post ( array (
     'post_type' => 'magazine',
     'post_title' => $title,
     'post_status' => 'publish',
    ));

    if ($post_catalog):
    update_field ('id_magazine', $id_catalog, $post_catalog);
    endif;

 endforeach;

}
add_action('init', 'my_api');
发布评论

评论列表(0)

  1. 暂无评论