List only first-level children of specific custom taxonomy term

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!

Solutions Collecting From Web of "List only first-level children of specific custom taxonomy term"

First of all you need the term id of hydro term, you can retrive it using get_term_by

$hydro = get_term_by('slug', 'hydro', 'product_cat');

After that, you can use the term id as 'parent' argument for get_terms

$hydro_children = get_terms( 'product_cat', array( 'parent' => $hydro->term_id ) );

Now you can display a list of this children:

if ( ! empty($hydro_children) ) {
  echo '<ul>';
  foreach( $hydro_children as $hydro_child ) {
    echo '<li><a href="' . get_term_link($hydro_child, 'product_cat') . '">';
    echo $hydro_child->name . '</a></li>';
  echo '</ul>';

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:

$args = array(
  'post_type' => 'product', // I guess
  'tax_query' => array(
      'taxonomy' => 'product_cat',
      'field' => 'id',
      'terms' => wp_list_pluck($hydro_children, 'term_id'),
      'include_children' => false
$query = new WP_Query( $args );