$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>categories - get_terms vs. get_categories: does it matter?|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

categories - get_terms vs. get_categories: does it matter?

matteradmin10PV0评论

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?

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?

Share Improve this question asked May 24, 2012 at 23:18 Stephen S.Stephen S. 1,0292 gold badges13 silver badges21 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 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.

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:

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

comment list (0)

  1. No comments so far