Wordpress API JSON 返回限制

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29171854/
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-03 17:48:19  来源:igfitidea点击:

Wordpress API JSON return limit

jsonwordpress

提问by Anthony Gawon Lee

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:

这是一个关于 Wordpress API /wp-json 的简单问题。我正在查询在 Wordpress 中使用特定类别过滤的一些数据。我的问题是如何控制从我的 Get 请求返回的结果数量...默认返回似乎返回大约 11 个最近的结果。有什么办法可以让它只返回 1 个(最近的),或者像 100 个帖子。我可以退货的最低和最高金额是多少。它的语法是什么。这是我的默认请求:

http://thisismywebsitewherewordpresslives.com/wp-json/posts?fiter[category_name]=Some Category Name I want to query&filter[order]=ASC

回答by apmeyer

If you're using v2 of the WordPress REST API, it looks like the current method of controlling the number of results returned is:

如果您使用的是 WordPress REST API 的 v2,则当前控制返回结果数量的方法看起来是:

website.com/wp-json/wp/v2/posts/?per_page=100

Replace 100 with the desired count.

将 100 替换为所需的计数。

If you're using a custom post type, replace posts with the custom post type. Also make sure to set 'show_in_rest' => truewhen configuring the custom post type.

如果您使用自定义帖子类型,请将帖子替换为自定义帖子类型。还要确保'show_in_rest' => true在配置自定义帖子类型时进行设置。

回答by Nithish Thomas

Add the filter[posts_per_page] parameter to the query to restrict the number of results returned by the API.

在查询中添加 filter[posts_per_page] 参数以限制 API 返回的结果数量。

http://thisismywebsitewherewordpresslives.com/wp-json/posts?filter[posts_per_page]=2&fiter[category_name]=Some Category Name I want to query&filter[order]=ASC

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

上面的查询应该只返回 2 个结果。查询参数列表在此处https://github.com/WP-API/WP-API/blob/master/docs/routes/routes.md#retrieve-posts

回答by l2aelba

As another said :(v2)

正如另一个人所说:v2

http://example.com/wp-json/wp/v2/posts?per_page=10

But If you want to get more next posts: (paged)

但是如果你想获得更多的下一篇文章:(分页

http://example.com/wp-json/wp/v2/posts?per_page=10&page=2

Docs :http://v2.wp-api.org/reference/posts/(Scroll to List Posts)

文档:http : //v2.wp-api.org/reference/posts/ (滚动到列表帖子

回答by apmeyer

My recommendation above is no longer correct. Now you'll want to use:

我上面的建议不再正确。现在你要使用:

website.com/wp-json/wp/v2/posts/?filter[posts_per_page]=100

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' => truewhen registering the custom post type.

更改数字以检索更多或更少的帖子。如有必要,将“帖子”更改为您的自定义帖子类型,并确保'show_in_rest' => true在注册自定义帖子类型时进行设置。

Note:While setting the posts_per_page value to -1 worked in earlier beta versions of the plugin (2.0-beta13.1), it does not seem to work in the most recent versions. Now it seems you must define a value in the endpoint.

注意:虽然将 posts_per_page 值设置为 -1 在插件的早期测试版 (2.0-beta13.1) 中有效,但它似乎不适用于最新版本。现在看来您必须在端点中定义一个值。

回答by D-Sims

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/2914

目前,API 强制执行 99 后限制返回。所以最大是website.com/wp-json/wp/v2/posts/?&per_page=99,也就是说你似乎可以修改以允许额外的帖子。这里有一个讨论:https: //github.com/WP-API/WP-API/issues/2914

回答by domdambrogia

I'm surprised no one mentioned using the native filters WordPress has created for situations exactly like this.

我很惊讶没有人提到使用 WordPress 为这种情况创建的原生过滤器。

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:

默认情况下,我能够实现返回所需数量的帖子,同时仍然允许$_GET['per_page']参数像这样工作:

/**
 * Default to all posts being returned rather than the default 10 posts per
 * page. Also, do not get in the way of the native $_GET['per_page'] setting.
 * @param {int} $newDefault  The new default amount of posts to return per paginated page
 * @var {void}
 */
function setRestApiPostsPerPage($newDefault) {
    foreach (get_post_types() as $i => $postType) {
        add_filter( "rest_{$postType}_query", function ($args, $request) {
            if (! isset($_GET['per_page'])) {
                // new default to overwrite the default 10
                $args['posts_per_page'] = $newDefault; 
            }

            return $args;
        }, 15, 2);
    }
}

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.

我确实发现你不能设置$args['posts_per_page'] = -1;这会导致抛出一个错误,你可以在这里看到

You can throw any logic within the closure/callback/filter to set the $args['posts_per_page']however you'd like to.

你可以在闭包/回调/过滤器中抛出任何逻辑来设置$args['posts_per_page']你想要的。

回答by Aaron Nusselbaum

You might change:

你可能会改变:

add_action( 'rest_YOUR_CPT_params', function($params){
    if ( isset( $params ) AND isset( $params[ 'per_page' ] ) ) {
        $params[ 'per_page' ][ 'maximum' ] = 500;
    }
    return $params;
});

And in fetch url add ?per_page=500

并在获取网址中添加 ?per_page=500

Example: https://domain.pl/wp-json/wp/v2/books?per_page=500

例子: https://domain.pl/wp-json/wp/v2/books?per_page=500

回答by Q Studio

The arg in V2 is "per_page" - http://v2.wp-api.org/reference/posts/

V2 中的参数是“per_page” - http://v2.wp-api.org/reference/posts/