I have a site set up with the following:
- Post Type: product
- Taxonomy: product_cat
- Taxonomy Slug: hydro
The term hydro has several immediate child terms and several grandchild terms.
I need to write a loop that will display only the first-level children of the custom taxonomy term “hydro” (i.e – skip grandchild terms), along with a permalink to their respective taxonomy archive pages.
I’ve tried working with get_terms()
in various incarnations but I just can’t get it right.
Any help is greatly appreciated. Thanks!
First of all you need the term id of hydro term, you can retrive it using
get_term_by
After that, you can use the term id as
'parent'
argument forget_terms
Now you can display a list of this children:
And/or run a post query for posts with
$hydro_children
terms attached, and if you want you can skip posts having only children of$hydro_children
terms (grand children of ‘hydro’) thanks to'include_children'
argument of tax query: