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

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:

Leave a Reply

Your email address will not be published. Required fields are marked *

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>