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?

1 Answer 1

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.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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