I’m fairly new to wordpress and am coming across new things each day – one was today when I happened across get_terms
and noticed that it was basically the same as get_category
.
Any particular reason to use one or the other? Is there something that I’m missing?
As you dive into WordPress, you’ll find that WordPress has a lot of wrapper functions. For instance, there’s
add_theme_page
that’s just a wrapper ofadd_submenu_page
. That’s certainly not the only example (add_submenu_page
itself has a bunch of wrappers, in fact). If you look at the source forget_categories()
, you’ll see that it too is a wrapper forget_terms()
(I just learned that myself, so thanks!).I find that the taxonomy-related functions are some of the most convoluted. A lot of them take very similar arguments and return similar things with little differences. In this case,
get_terms()
has aname__like
parameter thatget_category()
doesn’t. There are probably other little differences too.As a personal preference, I try to use
get_terms()
as much as possible. In some cases, likeadd_theme_page
that’s the recommended function (presumably so WordPress could make changes to the Theme page and keep that function working), but in other cases like this one, I don’t think it makes much of a difference. If nothing else, the familiarity helps me do more with it faster. However, some of the functions that return HTML lists likewp_list_categories()
can be useful at times.One of the most important (and not very obvious) differences between get_terms() and get_categories() is that get_categories() is a wrapper function for get_terms(‘category’). This means that you cannot get custom taxonomies with get_categories() and must use get_terms() instead.
It is possible to query custom taxonomy with
get_categories
example: