[wp-hackers] Getting lowest/last-level terms in hierarchical taxonomy

Haluk Karamete halukkaramete at gmail.com
Mon May 26 03:19:10 UTC 2014

HI Nikola
what if you walk thru all terms in that tax and check if the current term
has children or not.


Did I understand you correctly?
Here's an interesting one - how can I get all of the *last-level* terms in
a hierarchical taxonomy?

Take the following hierarchy for instance:

 - Food
 - - Vegetables
 - - - Cucumber
 - - - Carrot
 - - Bread
 - - Fruits
 - - - Citrus Fruits
 - - - - Orange
 - - - - Pomelo
 - Potato

( I know the potato doesn't make sense, but I just wanted to illustrate the
fact that some terms might be without children )

In this case, I'd want to grab the following terms: Cucumber, Carrot,
Bread, Orange, Pomelo, Potato.

I've been looking around I haven't found an easy solution so far.

I can think of two options - getting all terms and then recreating the tree
from them, or getting each level of terms and doing subsequent get_terms()
calls(which I don't like, since it's probably going to be very

My goal is to be able to present to the user a list of all last-level terms
in the taxonomy and then behind the scenes assign the post to all terms
from that tree(so, if you select Carrot, your post would be added to
Vegetables and Food).
I need to do that, since I have a custom table that stores post to post
relations and I need to be able to query for all items in a specific
category/list of categories with as little extra requests as possible(lots
of data is expected to be stored and performance is key).

wp-hackers mailing list
wp-hackers at lists.automattic.com

More information about the wp-hackers mailing list