Anthony Gawon Lee
Anthony Gawon Lee

Reputation: 375

Wordpress API JSON return limit

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:

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

Upvotes: 19

Views: 47417

Answers (10)

gjlmotea
gjlmotea

Reputation: 91

The easiest way (Overwrite it directly)

But this way, the default will be restored after updating the wordpress version.

You can search the WordPress repository for this string per_page' => array(
,then modify this 'maximum' value.

enter image description here

This will affect all queryable records, like posts, tags, categories and so on.

The second method (override value by function.php)

In \wp-content\themes\YourThemeInUse, find the function.php file, and write this code

<?php

// modify the maximum value
function modify_rest_any_collection_params( $params ) {
    if ( isset( $params['per_page'] ) ) {
        $params['per_page']['maximum'] = 1000;
    }
    return $params;
}

add_filter( 'rest_post_collection_params', 'modify_rest_any_collection_params', 10, 1 );
add_filter( 'rest_page_collection_params', 'modify_rest_any_collection_params', 10, 1 );
add_filter( 'rest_comment_collection_params', 'modify_rest_any_collection_params', 10, 1 );
add_filter( 'rest_user_collection_params', 'modify_rest_any_collection_params', 10, 1 );
add_filter( 'rest_taxonomy_collection_params', 'modify_rest_any_collection_params', 10, 1 );
add_filter( 'rest_term_collection_params', 'modify_rest_any_collection_params', 10, 1 );
add_filter( 'rest_post_tag_collection_params', 'modify_rest_any_collection_params', 10, 1 );

?>

Use rest_{post_type}_collection_params hook to override the collection parameters of a specific post type.

Third method (override value by custom plugin)

Just like method two.

Add custom-rest-api.php file in \wp-content\plugins\custom-rest-api, and enable this plugin.

<?php
/* ( This annotations block is required )
Plugin Name: Custom REST API Modifications
Description: Modify REST API parameters.
Version: 1.0
Author: Your Name
*/

// modify the maximum value
function modify_rest_any_collection_params( $params ) {
    if ( isset( $params['per_page'] ) ) {
        $params['per_page']['maximum'] = 1000;
    }
    return $params;
}

add_filter( 'rest_post_collection_params', 'modify_rest_any_collection_params', 10, 1 );
add_filter( 'rest_page_collection_params', 'modify_rest_any_collection_params', 10, 1 );
add_filter( 'rest_comment_collection_params', 'modify_rest_any_collection_params', 10, 1 );
add_filter( 'rest_user_collection_params', 'modify_rest_any_collection_params', 10, 1 );
add_filter( 'rest_taxonomy_collection_params', 'modify_rest_any_collection_params', 10, 1 );
add_filter( 'rest_term_collection_params', 'modify_rest_any_collection_params', 10, 1 );
add_filter( 'rest_post_tag_collection_params', 'modify_rest_any_collection_params', 10, 1 );

?>

Upvotes: 0

l2aelba
l2aelba

Reputation: 22147

As another said : (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 : https://developer.wordpress.org/rest-api/reference/posts/#list-posts

Upvotes: 10

domdambrogia
domdambrogia

Reputation: 2243

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

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:

/**
 * 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
 * @return {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.

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

Upvotes: 2

apmeyer
apmeyer

Reputation: 786

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

Note: The accepted solution is the way to go. This followup happened when these endpoints were evolving the accepted solution was broken temporarily.

Upvotes: 3

Rafał Jasiński
Rafał Jasiński

Reputation: 1

In reference to Aaron Nusselbaum answer but corrected a few things:

  • add_filter instead of add_action
  • filter name must contain additional '_collection', so 'rest_YOUR_CPT_collection_params'

For example:

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

Upvotes: 0

Damian Tokarczyk
Damian Tokarczyk

Reputation: 526

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

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

Upvotes: 3

D-Sims
D-Sims

Reputation: 66

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

Upvotes: 3

apmeyer
apmeyer

Reputation: 786

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

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

Replace 100 with the desired count.

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

Upvotes: 60

Q Studio
Q Studio

Reputation: 1831

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

Upvotes: 1

Nithish Thomas
Nithish Thomas

Reputation: 1565

Add the filter[posts_per_page] parameter to the query to restrict 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 query parameters are present here https://github.com/WP-API/WP-API/blob/master/docs/routes/routes.md#retrieve-posts

Upvotes: 12

Related Questions