Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wordpress REST API V2 return all posts

Tags:

rest

wordpress

I am missing the function to get ALL the posts (or CPT) by using

example.com/wp-json/wp/v2/country/?per_page=-1

or any similiar. The documentation gives as much info as this:

per_page: Maximum number of items to be returned in result set.

Default: 10

And in another question about the per_page we learn that the allowed range is 1 to 100.

In my case there will be a limited number of posts I need to get, but it will be around 200-300. Are there any workarounds to get them all other than fetching everything page per page and stitching it together?

Additional info if it does matter: I am using angular.js

like image 215
Jerry Avatar asked Mar 01 '16 17:03

Jerry


2 Answers

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;
}
like image 182
Tom Woodward Avatar answered Sep 21 '22 02:09

Tom Woodward


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.

like image 33
brunouno Avatar answered Sep 21 '22 02:09

brunouno