Wordpress REST API V2 返回所有帖子 [英] Wordpress REST API V2 return all posts
问题描述
我缺少使用
example.com/wp-json/wp/v2/country/?per_page=-1
或任何类似的.该文档提供了尽可能多的信息:
or any similiar. The documentation gives as much info as this:
per_page:结果集中返回的最大项目数.
per_page: Maximum number of items to be returned in result set.
默认值:10
在关于 per_page 的另一个问题中,我们了解到允许的范围是 1 到 100.
And in another question about the per_page we learn that the allowed range is 1 to 100.
就我而言,我需要获得的帖子数量有限,但会在 200-300 左右.除了获取每页的所有内容并将其拼接在一起之外,是否有任何解决方法可以让它们全部获得?
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?
如果确实重要,请提供其他信息:我正在使用 angular.js
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
像这样输入时,我得到的回报超过 100,并且可以将它们限制在 111 等.http://example.com/wp-json/wp/v2/posts/?filter[category_name]=country&filter[posts_per_page]=111
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
对于现代 WP 场景,以下函数将允许您提供大于 99 的回报.
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;
}
这篇关于Wordpress REST API V2 返回所有帖子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!