admin管理员组

文章数量:1292173

A client is using the WordPress REST JSON API with some WordPress data models. We need to hit the WordPress API from the frontend and retreive a few hundred custom posts.

Wordpress has a hard limit of 100 custom posts.

I would like to change this limit to a much higher number for this use case. I read that you cannot monkey patch in PHP.

Is there any way to adjust the per_page->maximum value to, say, 10000?

A client is using the WordPress REST JSON API with some WordPress data models. We need to hit the WordPress API from the frontend and retreive a few hundred custom posts.

Wordpress has a hard limit of 100 custom posts.

I would like to change this limit to a much higher number for this use case. I read that you cannot monkey patch in PHP.

Is there any way to adjust the per_page->maximum value to, say, 10000?

Share Improve this question edited Jul 8, 2017 at 17:44 kero 6,3201 gold badge25 silver badges34 bronze badges asked Jul 7, 2017 at 23:43 robbinttrobbintt 1311 silver badge4 bronze badges 2
  • Do you need to use this specific WordPress endpoint? If not, you could write a simple wrapper, which requests 100 pages until it is satisfied and returns the complete set – kero Commented Jul 8, 2017 at 11:52
  • @kero Do you have an example of such a wrapper in Javascript? It would at least solve the issue. – robbintt Commented Jul 8, 2017 at 15:53
Add a comment  | 

3 Answers 3

Reset to default 1

You cannot get over that limit of 100 posts per requests in WordPress for default requests. One way to still retrieve all posts would be to query that interface until you have all posts. Another would be a custom endpoint.

If you can, I suggest creating your own REST endpoint. This will already work and return all posts

add_action('rest_api_init', 'my_more_posts');

function my_more_posts() {
    register_rest_route('myplugin/v1', '/myposts', array(
        'methods' => 'GET',
        'callback' => 'my_more_posts_callback',
    ));
}

function my_more_posts_callback( WP_REST_Request $request ) {
    $args = array(
        'posts_per_page' => -1,
    );
    return get_posts($args);
}

More info on creating your own endpoint can be found here, whereas the arguments of get_posts() are explained here.


For a pure JavaScript solution in the frontend, you can utilize the x-wp-totalpages header which tells the total amount of pages. Unless this page is reached, we know we need to query again. So a basic recursive version like this works:

var allPosts = [];

function retrievePosts(opt) {
  if (typeof opt === 'undefined')
    opt = {};

  var options = $.extend({
    per_page: 100,
    page: 1
  }, opt);
  var url = 'https://example/wp-json/wp/v2/posts';
  var uri = url + '?per_page=' + options.per_page + '&page=' + options.page;

  $.getJSON(uri, function(data, status, jqXHR) {
    var totalpages = jqXHR.getResponseHeader('x-wp-totalpages');

    allPosts = allPosts.concat( data );

    // here the magic happens
    // if we are not done
    if (options.page < totalpages) {
      // request again, but for the next page
      retrievePosts({
        per_page: options.per_page,
        page: options.page + 1
      });
    } else {

      // WE ARE DONE
      // posts are in allPosts

    }
  });
};
retrievePosts();

This is still a problem in 2020, so I just edited the Wordpress source code on our server.

https://core.trac.wordpress/browser/trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-controller.php

In this file if you search for "100" you can easily find the configuration. I changed mine to 500.

I think Wordpress are right that this can cause front-end speed issues, but for those of us running Wordpress as a headless CMS to render another app (static site in our case) this is really annoying that you can't easily edit it.

There is a hook that can manipulate the per page maximum. The dynamic portion of the hook name, $post_type, refers to the post type slug

function increase_per_page_max($params){
    $params['per_page']['maximum'] = 1000;
    return $params;
}

add_filter('rest_{$post_type}_collection_params', 'increase_per_page_max');

Credit to https://wildwolf.name/how-to-increase-per_page-limit-in-wordpress-rest-api/

本文标签: get postsIncrease the page size of the WordPress REST API