Wordpress REST API V2 返回所有帖子

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35728943/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 11:11:57  来源:igfitidea点击:

Wordpress REST API V2 return all posts

wordpressrest

提问by Jerry

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

我缺少通过使用获取所有帖子(或 CPT)的功能

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

per_page:结果集中要返回的最大项目数。

默认值:10

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

另一个关于 per_page 的问题中,我们了解到允许的范围是 1 到 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?

就我而言,我需要获得的帖子数量有限,但会在 200-300 左右。除了获取每页的所有内容并将其拼接在一起之外,是否有任何解决方法可以让它们全部获得?

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

如果确实重要,请提供其他信息:我正在使用 angular.js

采纳答案by Tom Woodward

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

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

像这样输入时,我得到的回报超过 100,并且可以将它们限制在 111 等。 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.

对于现代 WP 场景,以下函数将允许您提供大于 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;
}

回答by jgangso

As of WP 4.7 you can increase the upper limit of WP REST API requests by hooking into the following filter:

从 WP 4.7 开始,您可以通过挂钩以下过滤器来增加 WP REST API 请求的上限:

rest_{$this->post_type}_collection_params

rest_{$this->post_type}_collection_params

This snippet should do the trick for posts:

此代码段应该可以解决帖子的问题

add_filter( 'rest_post_query', 'se35728943_change_post_per_page', 10, 2 );

function se35728943_change_post_per_page( $args, $request ) {
    $max = max( (int) $request->get_param( 'custom_per_page' ), 200 );
    $args['posts_per_page'] = $max;    
    return $args;
}

Note: you can not use standard per_pageargument (with value more then 100) in request - wp apiwill respond with error immediately (so the hook will not help). That's in the above code wee use custom_per_page(you can use any another word).

注意:您不能per_page在请求中使用标准参数(值大于 100) -wp api将立即响应错误(因此挂钩无济于事)。这是我们使用的上述代码custom_per_page(您可以使用任何其他词)。

Similar filter for taxonomies: rest_{$this->taxonomy}_query

分类法的类似过滤器:rest_{$this->taxonomy}_query

Example:

例子:

add_filter( 'rest_tags_query', 'se35728943_change_terms_per_page', 2, 10 );

function se35728943_change_terms_per_page( $prepared_args, $request ){
    $max = max( 200, (int) $request->get_param( 'custom_per_page' ) );

    $prepared_args['number'] = $max;
    return $prepared_args;
}

回答by brunouno

Couldn't get more than 10 with that syntax. Tried http://example.com/wp-json/wp/v2/posts?per_page=100and worked up to 100.

使用该语法无法获得超过 10 个。尝试http://example.com/wp-json/wp/v2/posts?per_page=100并工作到 100。

回答by daniel rubambura

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.

波纹管方法对我有用。我能够从一个包含 79 246 个帖子的站点中检索帖子。我使用了分页参数。在一个循环中,从 1 到 TotalPages 可用。请参阅文档的链接。

http://mydomain/wp-json/wp/v2/posts?_embed&per_page=100&page=793

per_page= 100 : means 100 posts max to be retrieved per page

per_page= 100 :表示每页最多可检索 100 个帖子

page= 793 : means i have 793 pages with 100 posts per page. the last page had only 46 posts

page= 793 :表示我有 793 页,每页 100 个帖子。最后一页只有 46 个帖子

A loop can then be used in a language of your choice.

然后可以在您选择的语言中使用循环。

回答by wp-overwatch.com

In WordPress 4.9.6, I had to use rest_{$this->post_type}_query

在 WordPress 4.9.6 中,我不得不使用 rest_{$this->post_type}_query

/* change amount of posts returned by REST API to 100 */
function rest_posts_per_page( $args, $request ) {
    $max = max( (int)$request->get_param( 'per_page' ), 100 );
    $args['posts_per_page'] = $max;
    return $args;
}
add_filter( 'rest_post_query', 'rest_posts_per_page', 10, 2 );

回答by Mulli

A simpler way that comply with protocol is to collect allresponses, in sequence, and perform singlesetState() call. As follows: (error handling omitted for readability)

遵守协议的一种更简单的方法是按顺序收集所有响应,并执行单个setState() 调用。如下:(为了可读性省略了错误处理)

componentDidMount(){

componentDidMount(){

var uri = "http://your-server";
var totalPages = 0;
var allResults = [];

fetch(uri)
.then(response => {
  totalPages = response.headers.get('X-WP-TotalPages');
  return response.json()})
.then(results => {
  allResults = results;
  //console.log('Got results from server', results.length); 
  for (let i = 2; i <= totalPages ; i++){
    fetch(uri + "?page=" + i)
    .then(response => {return response.json()})
    .then( moreresults => {
      allResults = allResults.concat( moreresults ); 
     });
  }
  this.setState({responses: allResults });
});

}

}

回答by Shimon S

It's really possible to get more then 100 posts or terms (tags, categories).
You should use rest_{$this->post_type}_queryhook (for posts) or rest_{$this->taxonomy}_queryhook for terms.
But you have to know,that it's impossible to pass a per_pagearg in your GETrequest with more then 100 value. WP APIwill 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 pagevalue in another getargument. And after this to trace this value in your hook.
So the code is:

真的有可能获得超过 100 个帖子或术语(标签、类别)。
您应该使用rest_{$this->post_type}_queryhook(用于帖子)或rest_{$this->taxonomy}_queryhook 用于术语。
但是您必须知道,per_page在您的GET请求中传递arg值超过 100是不可能的。WP API将立即抛出错误(钩子无济于事):(per_page must be between 1 (inclusive) and 100 (inclusive)具有 400 http 状态)。
要解决这个问题,您应该per page在另一个get参数中传递一个值。然后在你的钩子中追踪这个值。
所以代码是:

For posts

对于职位

add_filter( 'rest_post_query', 's976_rest_post_per_page', 2, 10 );
function s976_rest_post_per_page( array $args, WP_REST_Request $request ) {
    $post_per_page = $request->get_param('s976_per_page') ? $request->get_param('s976_per_page') : 10;
    $args['posts_per_page'] = $post_per_page;
    return $args;
}

For terms

对于条款

add_filter( 'rest_category_query', 's976_rest_cats_per_page', 2, 10 );
function s976_rest_cats_per_page( array $prepared_args, WP_REST_Request $request ){
    $cats_per_page = $request->get_param('s976_per_page') ? $request->get_param('s976_per_page') : 10;
    $prepared_args['number'] = $cats_per_page;
    return $prepared_args;
}

Of course, for this to work, you have to pass s976_per_page(with any value like 500, 99999) argument in GETrequest.

当然,要使其正常工作,您必须s976_per_pageGET请求中传递(使用任何值,如 500、99999)。

回答by JMB


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


我一直在成功地使用这个“s976_per_page”位代码,非常感谢,但它只返回“发布”标记的帖子,..
我怎么能得到它来获取所有状态,或者,只有一组/状态子集
非常感谢
JMB

回答by vr_driver

If you are planning on doing this with other parameters, you can also add categories etc as well. EG:

如果您计划使用其他参数执行此操作,您还可以添加类别等。例如:

https://www.website.com/wp-json/wp/v2/posts?categories=61&per_page=100