0

I need to get a list of all posts in a certain category. The number of posts is more than 100. I do not need the post's content. I need only id and slug.

https://example.com/wp-json/wp/v2/posts/ returns only 10 posts with content.

Is it possible to get all posts without content?

4
  • Pagination! You need to request the second page of data, etc, it's documented in the official docs with examples on wordpress.org
    – Tom J Nowell
    Jan 28, 2021 at 0:36
  • Yes. But in this case, I get also the content of the posts. I do not need so much data, I do not need to download all the content of the posts. I need only id and slug. Also, in this case, I need to generate several queries. Is it possible to get less data (only list, no content) in one query? Jan 28, 2021 at 3:12
  • 1
    I see, you'd be better asking 2 questions as 2 questions rather than bundling them, it reduces the likelihood of an answer as to post a solution you would need to know the answer to both questions. There's also an extremely high chance that the pagination question has already been asked and answered on the site. And the 1 question per question policy. Easy to understand focused and clear questions get more upvotes, visibility, views, and responses
    – Tom J Nowell
    Jan 28, 2021 at 11:00

2 Answers 2

4

Out of the box and using the core available hooks and API, you can't have more than 100 items per response on WordPress REST API for performance reasons. For the second part of the question, you may remove some fields from the response by using _fields parameter in your request as you can see in the examples of the handbook:

    // option a: using comma separated fields names.
    https://example.com/wp-json/wp/v2/posts/?_fields=author,id,excerpt,title,link

    // option b: using array syntax.
    https://example.com/wp-json//wp/v2/posts?_fields[]=author&_fields[]=id&_fields[]=excerpt&_fields[]=title&_fields[]=link

And theoretically if you own the website, you could remove fields from the API response by using the rest_prepare_{$this->post_type} dynamic filter for the post(s) type(s) you want to change.

if(!function_exists('wpse_382314_post_filter_data')) :
    function wpse_382314_post_filter_data($response, $post) {
        $response->data['post_title'] = '';
        $response->data['post_content'] = '';
    }
}

add_filter('rest_prepare_post', 'wpse_382314_post_filter_data', 10, 3);
0

First of all in order to get more than 10 posts all you need to do is to provide per_page parameter.

https://example.com/wp-json/wp/v2/posts/?per_page=100

But yes, sometimes there are could be thousands of posts, in that case of course it is not such a great idea to get everything in a single REST API request. And we can combine two requests by using both per_page and page parameter,

https://example.com/wp-json/wp/v2/posts/?per_page=100&page=1
https://example.com/wp-json/wp/v2/posts/?per_page=100&page=2

or you can also find the example with PHP and WordPress HTTP API here: https://rudrastyh.com/wordpress/rest-api-get-posts.html#parameters

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.