Only show parent term from custom taxonomy


How can I only show the top-level taxonomies when using a query loop to show posts?

So for example I have a custom post type called ‘stores’ and a custom taxonomy called ‘store category’
The store category example is built up like this:

  • DIY
    (-construction, -home, -etc)
  • Fashion
  • Outdoor
    (-climbing, running, hiking)

I have made query to show all the stores with name, location, etcetera. But I also want to show the ‘store category’ per “post” but only the main level. So only DIY, Fashion or Outdoor should been shown.

I think I need another query loop within the queryloop, but then? I can’t really figure it out yet.

Thanks for the help!


Maybe this can help:

1 Like

I have looked at that functionality and It would definitely help on the short term. But it can quickly become a big maintenance as more taxonomy categories get added. So I hope there is a more scalable solution available.

If I understand you correctly, you were trying to output the top level category for each loop right?

You should create a custom php function and use a basic text element to echo out.


function itchy_prod_first_level_categories() {
  global $product;
  if($product) {
	//Get the current product category
    $terms = get_the_terms( $product->get_id(), 'product_cat' );

    $cats = [];
	//Loop the terms and only save terms parent id is 0
    foreach( $terms as $term ) {
		if ($term->parent > 0) continue;
		$cats[] = $term->name;
	//Return the first parent term only
    return (count($cats) > 0) ? $cats[0] : '';

And I output this in my product loop template


Hi, yes of a custom taxonomy.
So I’ll have to tweak your code a bit. Will definitely try it.

sure, just change product_cat to your custom taxonomy

You can do this with the default Bricks query loop by setting the parent value to 0.


You probably fixed this by now, but for future visitors - you can do this with no code - on any page.
The ‘parent 0’ trick only works for main categories - if you go to a subcat, it breaks (because these have parents).
You can get ONLY the NEXT CHILD Categories by putting {echo:get_queried_object_id}
In both PARENT and CHILD OF - in the query.
In a category archive, this returns just same-level categories, enabling you to ‘walk down’ the category tree and show ‘next branches’ to go down - with a single template.

So on you store it would show DIY, FASHION, OUTDOOR
Go to OUTDOOR - and the same archive template shows climbing, running, hiking

If you had Hiking->boots, trousers, gear
Then on Hiking page it would show those… ad infinitum.


Hello to all,

How can we display only the child of a category?

Put the cat ID in Parent.

{echo:get_queried_object_id} works perfectly

In category archive, if you want to show the child of that category, just put CHILD OF - term ID.

What I would like to know is, how to do this, but when you are not on category archive. Like, I wanna do this in my mega menu… I am trying to make a query loop of terms, but one that only shows second level terms. Can’t figure it out…

@Anze that’s a more complicated ask because you need to check if the term has a parent AND that parent is top level. So you’d need to create your own helper function and then use the return… based on something like this:

But modified to return the complete string:

// Now we can loop again, and ONLY output terms who's parent are in the top-level id's (aka, second-level categories)
foreach( $categories as $category ) {
    // Only output if the parent_id is a TOP level id
    if( in_array( $category->parent_id, $top_level_ids )) {
        echo $category->term_id . ', ' . $category->slug . ', ' . $category->name . '<br />';

Becomes something like:

$html = "<ul>";
foreach( $categories as $category ) {
    if( in_array( $category->parent_id, $top_level_ids )) {
        $html .= "<li>" . $category->name . '</li>';
    $html .= "</ul>";
    return $html;

Only filled out with the link etc.

I think I actually managed to do what I wanted. I’m not even sure if it’s the right way. Basically what I did was created a query loop that display top parent terms by setting parent to 0 and then within that query loop I created another query loop that has CHILD of term_id. The inside query loop recognizes that it is in fact a child of the parent above it and properly displays only second level categories.

Now I started thinking if this would actually work if my second level categories had children of themselves (which they don’t and I don’t think they ever will on this page) but it’s breaking my brain. :rofl: …Actually it probably would work but the level 3 term would have to only have one parent assigned to it (the one above it).

1 Like

I’m using a Posts Query Loop but also want to deal with parent and child terms separately (currently it just spits out all terms in alphabetical order…). Post here:

Maybe someone can help—thanks!!