Wordpress REST API V2 return all posts

2020-02-23 08:09发布

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

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

And in another question about the per_page we learn that the allowed range is 1 to 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?

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

9条回答
Bombasti
2楼-- · 2020-02-23 08:48

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

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 });
});

}

查看更多
淡お忘
3楼-- · 2020-02-23 08:51

In WordPress 4.9.6, I had to use 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 );
查看更多
成全新的幸福
4楼-- · 2020-02-23 08:53

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

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 GET request.

查看更多
登录 后发表回答