Background
As discussed in Is There a Difference Between Taxonomies and Categories, taxonomies refer to a generalized system of classification and organization of objects or data. In the case of WordPress; Categories, Tags, Post Format(ting), and Link Categories are built in taxonomies used to help classify and organize posts, links, and metadata. Terms are instances of these taxonomies; take for example a WordPress blog about computer software. If one of the taxonomies is a category, then one of the terms would be programming languages, because programming languages is a topic in computer software, and could be considered a categorical taxonomy. Additonally, WordPress developers and Plugin developers may utilize the register_taxonomy()
function to initialize the built-in taxonomies, as well as create new taxonomies in WordPress.
Working with WordPress Categories
Ok, so WordPress offers up a few interesting functions under the Taxonomy API ( no codex page, links to most recent source code ). If I understand them correctly, some of the promising functions I can use for my purposes are the following:
get_terms()
: The codex pretty much says the same thing the source code comments say; that it retrieves the terms in a given taxonomy or list of taxonomies. I can provide an array of arguments to change which terms are returned; for example, to ensure that I include terms that are empty ( that is, are not referred to by any posts in the case of categories or tags ), I can set the hide empty argument to0
orfalse
;get_term_heirarchy()
: Returns all of the term IDs of a given term's children as an array.term_is_ancestor_of()
: Does just what it says; compare two terms, with term one being the term of reference, and term two the term to check for ancestry.
Umm, So What About Descendants
My goal here is to find out which categories don't have any child categories ( or descendant terms, to be more general ). I also don't want to ignore empty categories ( categories that have children but don't have any posts that reference them ).
Does WordPress have an easy way to do such a search? If so, are there performance concerns I should be worried about?
Some Edits: To provide an example for future reference, here is some hard data I am working with...
WordPress Category Admin View
Table of WordPress Taxonomy
Table of WordPress Terms
Table of WordPress Term_Relationships
In my test case, I want to return the following back:
- Uncategorized
- Firmware and Embedded Systems
- Operating Systems
- Web and Internet Based Applications
- Hardware
All of these have no children, even though some have an empty count.
find out which categories don't have any child categories
andBut I want to make sure that I include categories that have descendants even if one of those descendants is empty in my search.
Please clarify what exactly you want.