## posts – Use WP Rest API to paginate through all the pages

Question

So I have been attempting to get this working for some time now, but I can’t seem to figure it out, so I wanted to see if someone might be able to help me out.

So I have the following request:

if (!$option = get_option('TEST)) { return (object)[ 'page' => 1, 'started' => 0, 'finished' => 0, ]; }$response = wp_remote_get(
$this->global_endpoint . '?page=" .$settings->page .
"&per_page=" . 10 .
"&after=" . DateTime::init()->subtractTime("40 days')->format(
'Y-m-d\T00:00:01'
) .
'&exclude_postmeta_key=localsites_id'
);
$corporate_posts = json_decode($response['body'],
false,
512,
JSON_THROW_ON_ERROR
);


I’m able to get the header using the following:

$header = wp_remote_retrieve_headers($response);


Which gives me the following response:

[x-wp-total] => 53
[x-wp-totalpages] => 6


I’m doing a foreach to loop through all the posts using:

foreach ($corporate_posts as$corporate_post) {
$post_id = wp_insert_post([ 'id' =>$corporate_post->id,
'post_type' => 'post',
'post_status' => 'pending',
'post_title' => wp_strip_all_tags($corporate_post->title->rendered), 'post_content' => wp_strip_all_tags($corporate_post->content->rendered),
'post_excerpt' => wp_strip_all_tags($corporate_post->excerpt->rendered), 'post_author' => 0, 'post_date' =>$corporate_post->date,
'post_date_gmt' => \$corporate_post->date_gmt,
]);
}


How would I be able to do a pagination on the WordPress REST API?

0
2 weeks 2022-07-24T15:54:46-05:00 0 Answers 0 views 0