0

I have a custom taxonomy called product_category. There are child terms 3 levels down.

They look like this:

Term1
    Term2
        Term3

When I am on the taxonomy page for Term1, I wish to use get_term_children() to retrieve Term2, without retrieving Term3.

If that makes sense...

Can this be achieved with get_term_children() or is there another function I can use?

0

Presumably you want to output term data, so you can use get_terms with the child_of argument, then iterate over returned terms and check that the parent of each term is your main term id:

$parent_id = 42;
$children = get_terms( 'product_category', array( 'child_of' => $parent_id ) );

foreach( $children as $child ) {
    if( $parent_id == $child->parent )
        echo $child->name;
}

This will exclude grandchildren, as their parent term ID will be a child term, not your top level term.

  • That did the trick alright, thanks very much Milo. Perfect. – Eoghan OLoughlin Nov 20 '13 at 17:40
  • I need 3 more reputation to vote you up I'm afraid! – Eoghan OLoughlin Nov 20 '13 at 17:41
  • 1
    Is there a specific reason to use child_of instead of parent? – Schism May 27 '14 at 15:16
  • @Schism you're right, parent would be the simpler method! – Milo May 27 '14 at 17:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.