$hierarchies = get_taxonomy_hierarchy_multiple( array( 'category', 'post_tag' ) );
// or for a single taxonomy
$hierarchy = get_taxonomy_hierarchy( 'category' ); 

What’s going on?

During the first execution, we use get_terms() to gather all terms that do not have any parents ( parent = 0 ). Then, we loop through those terms, and look for terms whose parent is “this” term. Every time we loop through a set of terms, we look for children that belong to each term. Ultimately, we return an array of terms with nested children terms within the children property.

About the Author

Jonathan Daggerhart

Long time Drupal and WordPress developer. I like to write modules and plugins, and I dabble in frontend and design.

8 thoughts on “WordPress: Get taxonomy hierarchy, including children

Leave a Reply

Your email address will not be published. Required fields are marked *