7

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?

8

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 of add_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 for get_categories(), you'll see that it too is a wrapper for get_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 a name__like parameter that get_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, like add_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 like wp_list_categories() can be useful at times.

2

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.

  • Doh, this may not be true. Please disregard until I get an opportunity to verify a previous issue I ran in to. My apologies. – Brent Clark May 29 '12 at 18:58
  • 1
    Did you see the accepted answer that already explains this? – Chris_O May 29 '12 at 23:38
  • @BrentClark Did you ever find out if this was true? It's a more helpful answer to me. – Django Reinhardt Apr 4 '16 at 14:15
1

It is possible to query custom taxonomy with get_categories

example:

// Taxonomy query
$venue_args = array(
  'child_of'   => $venue_id,
  'taxonomy'   => 'wpmf-category',
  'hide_empty' => false
);
$venue_cats = get_categories( $venue_args );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.