最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

plugin development - How to get results of subcategories also?(WP Rest Api v2)

programmeradmin0浏览0评论

We can get posts from a category using below request

    `/wp/v2/posts?categories=1&search=somequery`

It only gives results from category(1) only. How can we get results from category(1) and from all subcategories of category(1) also? Than You.

We can get posts from a category using below request

    `/wp/v2/posts?categories=1&search=somequery`

It only gives results from category(1) only. How can we get results from category(1) and from all subcategories of category(1) also? Than You.

Share Improve this question asked Dec 12, 2016 at 7:20 Rushi JaganiRushi Jagani 332 silver badges6 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 1

This answer has what you want https://wordpress.stackexchange/a/314152/49962

WP already does this out of the box thanks to the include_children parameter of tax_query which is true by default.

So you do not need to tell it the sub-terms, just do something like this:

/wp/v2/posts?categories=1

Where categories=1 is the parent category ID

You'll need to do an additional call to find the IDs of the subcategories – there is no way to get posts from subcategories by only including the parent category in the search.

/wp/v2/categories?parent=ID&per_page=0&

This call will return an array of category objects, grab the IDs of those objects and pass them to your request for posts:

/wp/v2/posts?categories=1,A,B,C,D&search=somequery
发布评论

评论列表(0)

  1. 暂无评论