This is a simple question regarding WordPress API /wp-json. I am querying some data filtered with certain category in WordPress. My questions is how can I control amount of result that gets returned from my Get request… The default returns seems to return around 11 most recent results. Is there any way I can make it return only 1 (most recent), or like 100 posts. What is the minimum and maximum amount I can return. And what is the syntax for it. This is the default request I have:
http://thisismywebsitewherewordpresslives.com/wp-json/posts?fiter[category_name]=Some Category Name I want to query&filter[order]=ASC
If you’re using v2 of the WordPress REST API, it looks like the current method of controlling the number of results returned is:
Replace 100 with the desired count.
If you’re using a custom post type, replace posts with the custom post type. Also make sure to set
'show_in_rest' => true
when configuring the custom post type.Add the filter[posts_per_page] parameter to the query to restrict the number of results returned by the API.
The above query should return only 2 results. The list of query parameters are present here https://github.com/WP-API/WP-API/blob/master/docs/routes/routes.md#retrieve-posts
As another said : (v2)
But If you want to get more next posts : (paged)
Docs : http://v2.wp-api.org/reference/posts/ (Scroll to List Posts)
Currently the API imposes a 99 post limit return. So the max is
website.com/wp-json/wp/v2/posts/?&per_page=99
, that said it seems like you can modify to allow for additional posts. There’s a discussion on that here: https://github.com/WP-API/WP-API/issues/2914My recommendation above is no longer correct. Now you’ll want to use:
Change the number to retrieve more or fewer posts. Change “posts” to your custom post type if necessary and make sure to set
'show_in_rest' => true
when registering the custom post type.Note: The accepted solution is the way to go. This followup happened when these endpoints were evolving the accepted solution was broken temporarily.
You might change:
And in fetch url add
?per_page=500
Example:
https://domain.pl/wp-json/wp/v2/books?per_page=500
The arg in V2 is “per_page” – http://v2.wp-api.org/reference/posts/
I’m surprised no one mentioned using the native filters WordPress has created for situations exactly like this.
I was able to achieve returning a desired amount of posts by default, while still allowing the
$_GET['per_page']
param to work like so:I did find out you can’t set
$args['posts_per_page'] = -1;
That results in an error being thrown which you can see here.You can throw any logic within the closure/callback/filter to set the
$args['posts_per_page']
however you’d like to.In reference to Aaron Nusselbaum answer but corrected a few things:
For example: