pagination – Rest API Paginate until all posts are imported

Question

So I wanted to see if someone might be able to help me with this, so below I will explain what I’ve done:

  1. I am using wp_remote_get to grab the posts from another website with the arguments page and per_page.

  2. Next, I am checking to see if the post has already been imported using $current_post_id = post_exists($post->title->rendered); and if it does, don’t call the wp_insert_post function.

Here is what I have working already:

If I call the function, it will check those 100 posts on page 1 and check the titles and only import posts that are not matching.

What I’m trying to achieve:

  1. Let’s say that I have 402 posts, how do I dynamically loop through all the pages and then when it gets to the last page, reset back to the page 1?

Here is the code:

public function get_posts_via_rest_api(): void
{
    if (!is_admin()) {
        require_once(ABSPATH . 'wp-admin/includes/post.php');
    }
    $response = wp_remote_get(
        add_query_arg( [
            'page' => 1,
            'per_page' => 100
        ],
            'https://www.website-posts-live-at.com/wp-json/wp/v2/posts')
    );
    if (is_wp_error($response)) {
        return;
    }
    if ($response['response']['code'] === 200) {
        try {
            $posts = json_decode(
                $response['body'],
                false,
                512,
                JSON_THROW_ON_ERROR
            );
            if (empty($posts)) {
                return;
            }
        } catch (Exception) {
            error_log(
                print_r(json_last_error(), true)
            );
            return;
        }
        foreach ($posts as $post) {
            $current_post_id = post_exists($post->title->rendered);
            if ($current_post_id === 0) {
                $my_post = [
                    'post_type'     => 'post',
                    'post_status'   => 'pending',
                    'post_title'    => wp_strip_all_tags($post->title->rendered),
                    'post_content'  => wp_strip_all_tags($post->content->rendered),
                    'post_excerpt'  => wp_strip_all_tags($post->excerpt->rendered),
                    'post_author'   => 1
                ];
                $post_id = wp_insert_post($my_post);
                wp_set_object_terms($post_id, 'Global', 'category');
                wp_set_object_terms($post_id, 'Global', 'post_tag');
            }
        }
    }
}

0
Test2 5 months 2022-03-19T19:12:14-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse