As most of you would have already know, WordPress by default provides two built-in taxonomies – Categories and Tags. If you need to create a new association, then it is also very easy to create custom taxonomies in WordPress.
But one of the major limitations while creating custom taxonomies in WordPress is that you can’t make them as “single select”. By “single select” I mean the ability to restrict only one term to be selected for a post.
Following in one example where you might need this ability. Let’s say you are storing movie information in a custom post type and you need to store the PG rating for the movie in a custom taxonomy. Each movie will have only one rating and you should restrict that only one rating term is stored for a movie.
Recently I faced a similar scenario and this post talks about how I solved it.
Default Taxonomy types in WordPress
WordPress by default supports only two types of taxonomies.
- Hierarchical taxonomies, which are like categories. You can have multiple hierarchies and they are rendered using checkboxes.
- Non Hierarchical taxonomies, which are like tags. Here you create specify a list of comma separated taxonomy terms for a post.
As you might have already noted, in both the above two types you can’t restrict a post to have only one term selected from a given taxonomy.
Custom Field vs Custom Taxonomy
One option to solve this issue is to create a custom meta box and store the information in custom field.
This approach allows you to create your own UI, but if you need to retrieve posts based on the value in the field then you have to query your posts based on meta data which is generally slower than retrieving posts based on taxonomies.
Convert taxonomies into single select
The approach that I took to solve this problem is to create a custom meta box to display the taxonomies as radio buttons and then save the terms to the post as taxonomies when the post is saved.
Using a custom meta box to display the taxonomy
We can use a custom meta box for displaying the taxonomy instead of the built-in one by specifying the function that prints the meta box to the
meta_box_cb options of the
args parameter that is passed to the
And then inside the function we can fetch the term that is associated with the post and then display the radio buttons (or a dropdown).
Saving the term to the taxonomy
Now when a user saves the post, we can hook into the
save_post hook and then save the term as part of the taxonomy for that post.
Making sure the term is selected
If the term is mandatory then we can also send a error message back to the user (and change the post to draft) using the
edit_form_top hook if the user has not selected at least one term.
The entire source code is available as a gist. The code can be either added to your theme’s
functions.php file or as a separate plugin. Make sure you use the correct taxonomy name when you use this code.
- Email Log WordPress plugin v1.9.1 released
- Email Log WordPress plugin v1.9 released
- How to find if you are on the last page of a multi-page post in WordPress
- Easy Retweet WordPress plugin v3.1 Released
- Creating custom tables with correct Character Set and Collation in WordPress
- My contributions to WordPress 4.4