I am using per_page instead of the filter since it is no more available. but the problem is per_page value can be between 1 and 100. what if I want to pull records and it is more that 100. per_page will not return records if the count is more than 100. in the below API I want to pull the images which are more than 100 in count.
http://domain_name/wp-json/wp/v2/media/?per_page=100
Appreciate your help
Srikant
I am using per_page instead of the filter since it is no more available. but the problem is per_page value can be between 1 and 100. what if I want to pull records and it is more that 100. per_page will not return records if the count is more than 100. in the below API I want to pull the images which are more than 100 in count.
http://domain_name/wp-json/wp/v2/media/?per_page=100
Appreciate your help
Srikant
Share Improve this question asked Oct 10, 2016 at 15:45 Srikant BehraSrikant Behra 511 silver badge3 bronze badges 1- 1 A little late to the party but you can find the answer here: wordpress.stackexchange/q/281881/125601 – ssnepenthe Commented Nov 23, 2017 at 17:00
4 Answers
Reset to default 1Add a page number query to your request URL, and continue querying next page numbers until you get an empty result:
<your_wordpress_install_base_url>/wp-json/wp/v2/media/?per_page=100&page=2
Mark's suggestion to consider requesting less than 100 per page may be a good one. So for example you may want to do your query this way:
<your_wordpress_install_base_url>/wp-json/wp/v2/media/?per_page=25&page=3
Note also that (for me at least) these query URLs work if constructed as just ~/media?per_page=3
(instead of ~/media/?per_page=3
). I don't know whether one or the other is preferred.
Thanks, incidentally, for the /media/?per_page=100
part of your example. I'd been stumped about getting empty results when querying just /media
:)
You can add a route to your wordpress REST api. Make it so you can extract as many images as you need. You can copy the current route for getting the images and delete the maximum filter so you do not run into this issue.
Create new php file with following content and use it as a new endpoint:
header('Access-Control-Allow-Origin: *', 'Content-Type: application/json'); // Avoid cross origin block
$url1 = 'https://www.example/wp/v2/posts?per_page=100&page=1'; // path to your JSON file
$url2 = 'https://www.example/wp/v2/posts?per_page=100&page=2'; // path to your JSON file
$json1 = file_get_contents($url1);
$json2 = file_get_contents($url2);
$my_array1 = json_decode($json1, true);
$my_array2 = json_decode($json2, true);
$res = array_merge($my_array1, $my_array2);
$json_merge = json_encode($res);
echo $json_merge; // new json
And use it as a new endpoint. This is an example for 200 posts, you can add more input endpoints as you want.
Limits are there for a reason, before trying to overcome them you should ask yourself, "why is there a limit". The limit is there because if the response is unbounded everybody would be able to simply DOS any site by crashing the webserver or DB by asking to get all posts in one request.
Even without that consideration getting even 100 is likely to be slowish. If you want to use the REST API, just get the data in small amounts only when it is actually needed.
Still want to get more than 100? Write your own json end point and handler.