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 badges2 Answers
Reset to default 1This 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