admin管理员组文章数量:1125603
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.
/
returns only 10 posts with content.
Is it possible to get all posts without content?
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?
Share Improve this question asked Jan 28, 2021 at 0:02 Andrey EpifantsevAndrey Epifantsev 1031 gold badge1 silver badge3 bronze badges 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 ♦ Commented 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? – Andrey Epifantsev Commented 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 ♦ Commented Jan 28, 2021 at 11:00
- WWW wordpress.stackexchange.com/questions/382314/… wordpress.stackexchange.com/questions/382314/… – WTF Commented Sep 27, 2023 at 12:00
2 Answers
Reset to default 4Out 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);
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
本文标签:
版权声明:本文标题:Wordpress REST API V2: how to get list of all posts? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736600485a1945210.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论