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
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.
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:
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 usecustom_per_page
(you can use any another word).Similar filter for taxonomies:
rest_{$this->taxonomy}_query
Example:
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.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.
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.
A simpler way that comply with protocol is to collect all responses, in sequence,
and perform single
setState()
call. As follows: (error handling omitted for readability)In WordPress 4.9.6, I had to use
rest_{$this->post_type}_query
I hope this help, I’m using axios but is the same other
If you are planning on doing this with other parameters, you can also add categories etc as well. EG:
It’s really possible to get more then 100 posts or terms (tags, categories).
You should use
rest_{$this->post_type}_query
hook (for posts) orrest_{$this->taxonomy}_query
hook for terms.But you have to know, that it’s impossible to pass a
per_page
arg in yourGET
request with more then 100 value.WP API
will throw an error immediately (the hook will not help):per_page must be between 1 (inclusive) and 100 (inclusive)
(with 400 http status).To get around this problem you should pass a
per page
value in anotherget
argument. And after this to trace this value in your hook.So the code is:
For posts
For terms
Of course, for this to work, you have to pass
s976_per_page
(with any value like 500, 99999) argument inGET
request.I have been using this ‘s976_per_page’ bit of code successfully so many thanks,but It only returns the ‘publish’ tagged posts,..
How could I get this to grab ALL statuses, or, alternatively, only a set / subset of statuses
Many Thanks
JMB
This solution with javascript worked for me.
According to documents, 100 is maximum to get. If you want more, use offsets and multiple requests. Example:
Try this, this works fine (WP 5.8.1)