WordPress REST API V2: how to get list of all posts?

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?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x