Skip to Main Content

Use category interface with non-hierarchical custom taxonomies

When creating a custom taxonomy in WordPress, you get a different default interface for hierarchical taxonomies than for non-hierarchical taxonomies.  Hierarchical taxonomies get a checkbox-based interface, like Categories, while non-hierarchical taxonomies get an auto-complete interface, like Tags.

taxonomy callbacks

ADVERTISEMENT:

You can select the metabox interface used with the ‘meta_box_cb’ attribute of the ‘register_taxonomy’ call.  By default, hierarchical taxonomies use the callback ‘post_categories_meta_box’ and non-hierarchical taxonomies use the callback ‘post_tags_metabox’.  Unfortunately, the two options aren’t compatible with each other.

For some reason, when a post is saved or updated, non-hierarchical taxonomies expect to receive an array or comma-separated list of term names (as strings), while hierarchical taxonomies expect to receive an array of term ids (as ints).  Luckily, it is fairly trivial to translate between the two formats.

Here’s the code to make a non-hierarchical taxonomy that uses the category-style metabox:

3 Comments

  1. Steve's profile image.

    Hello, I am trying to do this very thing and everything looks right, however after selecting a few terms from my custom taxonomy and clicking the Update button to save, all terms in that taxonomy are cleared completely and not saved to the post. Any thoughts? Does this solution still work? Thanks!

Your email address will not be published.

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>