Wordpress API JSON Return Limit

This is a simple question about the Wordpress API / wp-json. I request some data filtered by a specific category in Wordpress. My questions are: how can I control the amount of result that is returned from my Get request ... By default, apparently about 11 last results are returned. Is there a way to make it return only 1 (last) or 100 messages. What is the minimum and maximum amount that I can return. And what is the syntax for. This is the default query that I have:

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

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

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

Replace 100 with your desired amount.

If you are using a custom message type, replace the message with a special message type. Also remember to set 'show_in_rest' => true when setting up a custom message type.

+35
source

Add the filter [posts_per_page] parameter to the query to limit the number of results returned by the 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 request parameters is here https://github.com/WP-API/WP-API/blob/master/docs/routes/routes.md#retrieve-posts

+12
source

As another said: (v2)

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

But if you want to receive more of the following messages : (paged)

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

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

+7
source

The API is currently imposing a 99 percent return. Thus, max website.com/wp-json/wp/v2/posts/?&per_page=99 , which said that it seems that you can change to add additional posts. The following is discussed here: https://github.com/WP-API/WP-API/issues/2914

+3
source

My recommendation above is no longer true. Now you want to use:

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

Change the number to receive more or less messages. If necessary, change the "messages" to your custom message type and remember to set 'show_in_rest' => true when registering a custom message type.

Note. . When setting posts_per_page -1 in the earlier beta versions of the plugin (2.0-beta13.1), it seems that it does not work in the latest versions. Now it seems that you should define the value at the endpoint.

+2
source

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

+1
source

I am surprised that no one mentioned using their own filters created by WordPress for such situations.

I managed to get the desired number of posts back by default, while allowing the $_GET['per_page'] parameter to work like this:

 /** * 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 found that you cannot set $args['posts_per_page'] = -1; This results in an error that you can see here .

You can add any logic to the closure / callback / filter to set $args['posts_per_page'] as you would like.

+1
source

You can 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 the URL to get add ?per_page=500

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

0
source

Source: https://habr.com/ru/post/984057/


All Articles