get_term_children doesn’t return an array of children terms

I am trying to get the term children and then check if they exist to display them. But I get no IDs in return of get_term_children command. This is the code:

  $children = get_term_children($term->term_id, locations);
  echo $children;

$term is already defined and outputs well, “locations” taxonomy exists. The code returns me just an “Array” word, no list of child terms shows up.

Read More

I’ve got a similar code in my post archive pages and it works well returning me child categories in n/n/n/n/n format:

  $children = get_category_children($this_category->cat_ID);
  echo $children;

What’s wrong with the first code? It should work as well as the second.

Related posts

Leave a Reply

1 comment

  1. At first, try passing the Taxonomy as a string:

    $children = get_term_children($term->term_id, 'locations');
    

    The function get_category_children produces a string as return, so you can just echo it.
    the function get_term_children, however, returns an array. To see the contents of the array, try

    print_r( $children );
    

    instead of echo.

    The last thing to keep in mind is that a taxonomy does not need to be hierarchical – for example the built in tags. Be sure your “locations” is organized hierarchical, otherwise get_term_children won’t work for you in this case.