WordPress: Get taxonomy hierarchy, including children

[pastacode lang=”php” manual=”%3C%3Fphp%0A%0A%2F**%0A%20*%20Recursively%20get%20taxonomy%20and%20its%20children%0A%20*%0A%20*%20%40param%20string%20%24taxonomy%20Taxonomy%20slug.%0A%20*%20%40param%20int%20%24parent%20Parent%20term%20id.%0A%20*%20%40return%20array%0A%20*%2F%0Afunction%20get_taxonomy_hierarchy(%20%24taxonomy%2C%20%24parent%20%3D%200%20)%20%7B%0A%20%20%20%20%2F%2F%20Only%201%20taxonomy.%0A%20%20%20%20%24taxonomy%20%3D%20is_array(%20%24taxonomy%20)%20%3F%20array_shift(%20%24taxonomy%20)%20%3A%20%24taxonomy%3B%0A%20%20%20%20%2F%2F%20Get%20all%20direct%20descendants%20of%20the%20%24parent.%0A%20%20%20%20%24terms%20%3D%20get_terms(%20%24taxonomy%2C%20%5B%20’parent’%20%3D%3E%20%24parent%20%5D%20)%3B%0A%20%20%20%20%2F%2F%20Prepare%20a%20new%20array.%20These%20are%20the%20children%20of%20%24parent.%0A%20%20%20%20%2F%2F%20We’ll%20ultimately%20copy%20all%20the%20%24terms%20into%20this%20new%20array%2C%20but%20only%20after%20they%0A%20%20%20%20%2F%2F%20find%20their%20own%20children%0A%20%20%20%20%24children%20%3D%20%5B%5D%3B%0A%20%20%20%20%2F%2F%20Go%20through%20all%20the%20direct%20descendants%20of%20%24parent%2C%20and%20gather%20their%20children.%0A%20%20%20%20foreach%20(%20%24terms%20as%20%24term%20)%7B%0A%20%20%20%20%20%20%20%20%2F%2F%20Recurse%20to%20get%20the%20direct%20descendants%20of%20%22this%22%20term.%0A%20%20%20%20%20%20%20%20%24term-%3Echildren%20%3D%20get_taxonomy_hierarchy(%20%24taxonomy%2C%20%24term-%3Eterm_id%20)%3B%0A%20%20%20%20%20%20%20%20%2F%2F%20Add%20the%20term%20to%20our%20new%20array.%0A%20%20%20%20%20%20%20%20%24children%5B%20%24term-%3Eterm_id%20%5D%20%3D%20%24term%3B%0A%20%20%20%20%7D%0A%20%20%20%20%2F%2F%20Send%20the%20results%20back%20to%20the%20caller.%0A%20%20%20%20return%20%24children%3B%0A%7D%0A%2F**%0A%20*%20Recursively%20get%20given%20taxonomies’%20terms%20as%20complete%20hierarchies.%0A%20*%0A%20*%20%40param%20string%7Carray%20%24taxonomies%20Taxonomy%20slugs.%0A%20*%20%40param%20int%20%24parent%20-%20Starting%20parent%20term%20id%0A%20*%0A%20*%20%40return%20array%0A%20*%2F%0Afunction%20get_taxonomy_hierarchy_multiple(%20%24taxonomies%2C%20int%20%24parent%20%3D%200%20)%20%7B%0A%20%20%20%20if%20(%20!%20is_array(%20%24taxonomies%20)%20%20)%20%7B%0A%20%20%20%20%20%20%20%20%24taxonomies%20%3D%20%5B%20%24taxonomies%20%5D%3B%0A%20%20%20%20%7D%0A%20%20%20%20%24results%20%3D%20%5B%5D%3B%0A%20%20%20%20foreach(%20%24taxonomies%20as%20%24taxonomy%20)%7B%0A%20%20%20%20%20%20%20%20%24terms%20%3D%20get_taxonomy_hierarchy(%20%24taxonomy%2C%20%24parent%20)%3B%0A%20%20%20%20%20%20%20%20if%20(%20%24terms%20)%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%24results%5B%20%24taxonomy%20%5D%20%3D%20%24terms%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%20%20%20%20return%20%24results%3B%0A%7D” message=”” highlight=”” provider=”manual”/]

 

Usage:

[pastacode lang=”php” manual=”%24hierarchies%20%3D%20get_taxonomy_hierarchy_multiple(%20%5B%20’category’%2C%20’post_tag’%20%5D%20)%3B%0A%2F%2F%20or%20for%20a%20single%20taxonomy%0A%24hierarchy%20%3D%20get_taxonomy_hierarchy(%20’category’%20)%3B%20″ message=”” highlight=”” provider=”manual”/]

What’s going on?

  1. During the first execution, we use get_terms() to gather all terms that do not have any parents ( parent = 0 ).
  2. Then, we loop through those terms and get each of their children by calling the get_taxonomy_hierarchy() on the terms.
  3. Every time we loop through a set of terms, we look for children that belong to each term.
  4. Finally, we return an array of terms with all it’s children stored within a new $term->children property.
11 Thoughts

Discussion

Michael
July 3, 2015

Nice, thanks.

Alexander Khmelnitskiy
November 28, 2016

Thanks mate!
Exactly what I need.

sasasasa
December 1, 2016

I get “Warning: Missing argument 1 for get_taxonomy_hierarchy()”

Jonathan Daggerhart
December 2, 2016

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
January 8, 2017

Thanks, saved me a lot of time.

Biswajit Paul
May 26, 2017

Nice work.

Carlos Souza
October 5, 2017

Thanks man, you saved me a lot of time and work :)

Robert Wheeler
October 24, 2017

Works great, very succinct code. Thank you for sharing!

Myles McNamara
October 23, 2018

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

Darren Cooney
April 12, 2019

Thanks Jonathon, This saved the day for me :)

Steven Ayers
June 16, 2020

Jonathan, nice functions! This saved me a bunch of time. I hope things are going well on your side of Asheville.

Aaron
September 18, 2020

Five years later and still the best solution I could stumble upon. I’m working on a variation that will allow me to pass a parent term and have it iterate only through that term’s children, I think you’ve set me up easily to do that. Thanks!

Ray
March 15, 2023

Hey, would you mind to share your code? Im looking for exactly that ;)

Leave a Reply

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