Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

The question is "What is the Difference Between Hierarchical and Non-Hierarchical Taxonomies?" This question really stumped me at first so I figured it would be a good idea to show the difference to others surfing the site looking for the distinction.

Specifically the question is referring to the hierarchical argument passed to the register_taxonomy() function. More specifically, what's the difference between this:

'hierarchical' => false

register_taxonomy('movie-genre', 'movie', array(
  'hierarchical'    => false,
  'label'           => 'Genre',
  'query_var'       => 'movie-genre',
  'rewrite'         => array('slug' => 'genres' ),
));

And this?

'hierarchical' => true

register_taxonomy('movie-genre', 'movie', array(
  'hierarchical'    => true,
  'label'           => 'Genre',
  'query_var'       => 'movie-genre',
  'rewrite'         => array('slug' => 'genres' ),
));

Note I'm going to go ahead and answer my own question but won't mark it as best unless nobody else steps up with a really good answer too. Also my gut feeling tells me I might not have capture every distinction between the two dichotomies so if not please let us know what I missed.

share|improve this question
    
Can you also explain what is hierarchical to what? Is the taxonomy-group hierarchical to the post type? Or is the individual taxonomy hierarchical to the taxonomy-group? I'm just thrown off by the word hierarchical I guess. Thanks. –  Dave Jan 24 '12 at 20:28

1 Answer 1

up vote 24 down vote accepted

The simple answer is that terms in hierarchical taxonomies can have child terms. But what else?

'hierarchical'=>false

When you specify a 'hierarchical'=>false you get the following type of metabox which is the metabox format WordPress also uses for Post Tags:

Taxonomy Term Metabox in the Post Edit Screen when $hierarchical==false

'hierarchical'=>true

However when you specify 'hierarchical'=>true you get the following type of metabox which is the metabox format WordPress also uses for Categories:

Taxonomy Term Metabox in the Post Edit Screen when $hierarchical==true

Of course the example above also points out where hierarchical categorization can be a bit of a mixed bag because in real life subcategories often apply to many parent categories. Even so alowing "many parents" is not how hierarchical taxonomies works in WordPress but IMO categorizing anything perfectly is almost impossible regardless of how WordPress works. So Caveat Emptor!

On Custom Taxonomy Registration, or "Why Won't it Save?"

While not directly related to the question if you are a newbie trying out custom taxonomies, (or an experienced dev who isn't paying attention like happened to me when I wrote this up!) it's likely that you'll try adding register_taxonomy() like the code you see in the question directly into your theme's functions.php file. Oops!

If you do add the code directly into functions.php your metabox will display but it won't save your newly added terms (and in the 'heirarchical'=>true form of the metabox your existing terms won't load with checkboxes.) That's because you need to register custom taxonomies (and custom post types) inside an init hook, like so:

<?php
add_action('init','register_movie_genre_taxonomy');
  function register_movie_genre_taxonomy() {
    register_taxonomy('movie-genre', 'movie', array(
      'hierarchical'    => true,
      'label'           => 'Movie Genre',
      'query_var'       => 'movie-genre',
      'rewrite'         => array('slug' => 'genres' ),
    ));
}

Hope this helps!

share|improve this answer
    
Is a Hierarchical Taxonomy the same thing as a custom category? And is a Non-Hierarchical Taxonomy the same thing as a custom tag? –  Daniel Jun 3 at 15:11
1  
@ravensfan55222 - Not exactly, but almost. Instead of a 'hierarchical taxonomy' being the same as a custom category, the built-in Category classification system is just one of many potential hierarchical taxonomies. Similarly (Post) Tags are just one example of a non-hierarchical taxonomy. More correctly, "hierarchical" is just an attribute of a taxonomy, like how a label is an just attribute of a taxonomy or color is an attribute of a car. That might have been what you meant but I wanted to clarify if not. –  MikeSchinkel Jun 7 at 3:17

protected by toscho Jun 8 '12 at 11:20

Thank you for your interest in this question. Because it has attracted low-quality answers, posting an answer now requires 10 reputation on this site.

Would you like to answer one of these unanswered questions instead?

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