<?php
/**
* Recursively get taxonomy and its children
*
* @param string $taxonomy
* @param int $parent - parent term id
* @return array
*/
function get_taxonomy_hierarchy( $taxonomy, $parent = 0 ) {
// only 1 taxonomy
$taxonomy = is_array( $taxonomy ) ? array_shift( $taxonomy ) : $taxonomy;
// get all direct decendants of the $parent
$terms = get_terms( $taxonomy, array( 'parent' => $parent ) );
// prepare a new array. these are the children of $parent
// we'll ultimately copy all the $terms into this new array, but only after they
// find their own children
$children = array();
// go through all the direct decendants of $parent, and gather their children
foreach ( $terms as $term ){
// recurse to get the direct decendants of "this" term
$term->children = get_taxonomy_hierarchy( $taxonomy, $term->term_id );
// add the term to our new array
$children[ $term->term_id ] = $term;
}
// send the results back to the caller
return $children;
}
/**
* Recursively get all taxonomies as complete hierarchies
*
* @param $taxonomies array of taxonomy slugs
* @param $parent int - Starting parent term id
*
* @return array
*/
function get_taxonomy_hierarchy_multiple( $taxonomies, $parent = 0 ) {
if ( ! is_array( $taxonomies ) ) {
$taxonomies = array( $taxonomies );
}
$results = array();
foreach( $taxonomies as $taxonomy ){
$terms = get_taxonomy_hierarchy( $taxonomy, $parent );
if ( $terms ) {
$results[ $taxonomy ] = $terms;
}
}
return $results;
}
Usage:
$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.
9 thoughts on “WordPress: Get taxonomy hierarchy, including children”
Michael
Nice, thanks.
Alexander Khmelnitskiy
Thanks mate!
Exactly what I need.
sasasasa
I get “Warning: Missing argument 1 for get_taxonomy_hierarchy()”
Jonathan Daggerhart
Sounds like you need to specify which taxonomy you’re trying to get. The core WordPress taxonomies are named “category” and “post_tag”.
Andrew Robbins
Thanks, saved me a lot of time.
Biswajit Paul
Nice work.
Carlos Souza
Thanks man, you saved me a lot of time and work :)
Robert Wheeler
Works great, very succinct code. Thank you for sharing!
Myles McNamara
Great stuff, thank you! For those who want to return all taxonomies (even those without any posts), add `hide_empty` to the args passed. I also updated this example code to add the ability to pass an array of args as 3rd argument, to override the default ones, or add any specific ones, you can find at this gist:
https://gist.github.com/tripflex/631610f46350edf4b84d88dad75e6517