Wordpress REST API V2 return all posts

As of WP 4.7 you can increase the upper limit of WP REST API requests by hooking into the following filter:

rest_{$this->post_type}_collection_params

This snippet should do the trick for posts:

add_filter( 'rest_post_query', 'se35728943_change_post_per_page', 10, 2 );

function se35728943_change_post_per_page( $args, $request ) {
    $max = max( (int) $request->get_param( 'custom_per_page' ), 200 );
    $args['posts_per_page'] = $max;    
    return $args;
}

Note: you can not use standard per_page argument (with value more then 100) in request - wp api will respond with error immediately (so the hook will not help). That's in the above code wee use custom_per_page (you can use any another word).

Similar filter for taxonomies: rest_{$this->taxonomy}_query

Example:

add_filter( 'rest_tags_query', 'se35728943_change_terms_per_page', 2, 10 );

function se35728943_change_terms_per_page( $prepared_args, $request ){
    $max = max( 200, (int) $request->get_param( 'custom_per_page' ) );

    $prepared_args['number'] = $max;
    return $prepared_args;
}

Couldn't get more than 10 with that syntax. Tried http://example.com/wp-json/wp/v2/posts?per_page=100 and worked up to 100.


Try this instead for pagination. It returns all the posts on my site.

http://example.com/wp-json/wp/v2/posts/?filter[category_name]=country&filter[posts_per_page]=-1

I get returns above 100 when entered like that and can cap them at 111 etc. http://example.com/wp-json/wp/v2/posts/?filter[category_name]=country&filter[posts_per_page]=111

For the modern WP scenarios the following function will allow you to give returns great than 99.

add_filter( 'rest_post_collection_params', 'big_json_change_post_per_page', 10, 1 );
function big_json_change_post_per_page( $params ) {
    if ( isset( $params['per_page'] ) ) {
        $params['per_page']['maximum'] = 200;
    }
    return $params;
}

The bellow approch worked for me. I was able to retrieve posts from a site with 79 246 posts. I used pagination parameters . Within a loop, from 1 to TotalPages available. See the link for doc.

http://mydomain/wp-json/wp/v2/posts?_embed&per_page=100&page=793

per_page = 100 : means 100 posts max to be retrieved per page

page = 793 : means i have 793 pages with 100 posts per page. the last page had only 46 posts

A loop can then be used in a language of your choice.

Tags:

Rest

Wordpress