A Quick Guide to Creating Custom Taxonomies in WordPress
The most frequent questions I get asked about WordPress are usually about custom post types (which I covered in How to Create WordPress Custom Post Types) and custom taxonomies, which we’re going to tackle today.
Custom taxonomies were introduced in WordPress 3.0 as a core feature and allow you to group posts together with more than just tags and categories. For example, if you run a website about books or movies, you could use a custom taxonomy named “Genre,” and if you write about sports you could use a taxonomy called “Sport.”
In this post, we’ll look at what custom taxonomies are, how to use them and how you can create your own.
What Are Categories, Tags, Taxonomies and Terms?
There is a lot of confusion about the difference between tags and categories, and how taxonomies fit in with all of this. The confusion arises from the fact that we use the word “categorize” when sorting items into categories but we don’t really have a separate word for sorting items into tags or other taxonomies (tagize?). Therefore, whenever we split items into groups we refer to this as “categorization.”
The word “taxonomy” is an umbrella term for a way to sort items based on a similar property. Terms are members of a taxonomy, the similar property based on which items are sorted. “Category” and “Tag” are both a kind of taxonomy. Specific categories and tags are terms inside a taxonomy. If this isn’t clear just yet don’t worry, keep reading and it will soon become clearer!
Technically, there is no difference between categories and tags, only the way we use them by custom. Most people use only a couple of categories, which makes sense because it makes our website much easier to browse. We usually go nuts with tags, adding a couple to each post.
Technically, there is no difference between categories and tags, only the way we use them by custom.
WordPress defines a category as being hierarchical. This means that a category can be a parent of another category. For example, You could have “Art” as a top level category and “Photography”, “Illustration” and “Painting” as sub-categories. Tags are non-hierarchical. In the admin this is reflected in the user interface.
Categories are shown in a larger box with each category listed and positioned in the correct hierarchy. They can be checked and unchecked to achieve the final category structure. Tags are more free-form. They can be loosely types with suggestions and multiple ones can be added using a comma.
Taxonomies are defined by a theme or a plugin and can be set to any type you wish. Sticking to the sports blog example, perhaps you have a hierarchical taxonomy named “Sport.” This contains top level terms such as “Martial Arts,” “Ball Sports” and “Athletics.” The martial arts term may have some sub-terms such as “Karate” and “Taekwondo.”
You may also want to use the name of athletes you mention as a taxonomy to be able to easily list news items related to those people. You could create a non-hierarchical taxonomy for this. The terms inside this taxonomy could be “Michael Jordan,” “Michael Schumacher” or “Usain Bolt.”
It’s important to understand that while there are good practices here, the way you use taxonomies is up to you. A news website may use categories to divide content into “Sports,” “Business” and “Other.” Another news website may use the same division, but may name their taxonomy “News Types” instead of “Category.”
You’ll need to write some code to create taxonomies but never fear, I’ll be giving you examples you can copy and paste. You can add the code you find in this article in three locations. The best place to put it is in a plugin. You can learn about creating a plugin in the WordPress Codex.
If you are using a third party theme then you should create a child theme to make sure you don’t modify the theme’s original files. Check out our article on How To Create A Child Theme.
The easiest, but least recommended way is to use your theme’s functions.php file. If you’re just experimenting, feel free to use this file, just be aware that if you update your theme, the changes you make will be lost.
We can easily create a taxonomy using the register_taxonomy() function. Here’s a basic example:
In the WordPress admin it looks like this:
This is actually all we need to do to get a fully functional taxonomy. We can create a non-hierarchical taxonomy by changing the hierarchical property to false. Let’s do that now by creating a taxonomy where we can assign athletes to our posts:
And here’s what it looks like in the admin:
Both examples are great, but there is so much more we could do. First of all, take a look at the screenshots and note the wording. “Add new category,” “Choose from the most used tags,” etc.
The text does not change to fit the name of our taxonomy by itself. Let’s take a look at some of the advanced options we have at our disposal.
The register_taxonomy() function takes three parameters. The first parameter is the slug of the taxonomy. The second is either a string that defines the slug of the post type it should be added to, or an array of strings defining a number of post types. The third parameter is an array of properties that is used to set up the taxonomy.
The first thing we’ll add to this array of properties is the labels used. This will tailor all the text in relation with the taxonomy.
The above function replaces our original function as is. The only thing I’ve added is the labels, but it’s a pretty sizable array due to all the options. For all future examples I will omit the labels for brevity’s sake.
Sometimes you may want users to see your taxonomies, other times you may just want to use them internally. The arguments allow you to control all this in quite some detail. Here are the available parameters and their possible values:
- public: This parameter sets a number of other parameters in one go. If it is set to true the taxonomy will be publicly queryable, which means that it is visible to users in the front-end. If set to false it will only be used internally.
- show_ui: When set to false the box in the screenshots above is not shown. Taxonomies can be added, retrieved and used via code, but not via a user interface. By default the value of this parameter is the same as the value set for the public parameter.
- show_in_nav_menus: If this parameter is set to false, terms will not be selectable in the menu manager section of WordPress. By default the value of this parameter is the same as the value set for the public parameter.
- show_tagcloud: This parameters sets weather the tag cloud widget can use this taxonomy. By default the value of this parameter is the same as the value set for the public parameter.
- show_admin_column: If set to true the taxonomy will be visible in the post list of the post type it is attached to. By default the value of this parameter is false.
I mentioned hierarchy in the opening section. The hierarchy parameter can be set to true or false. By default taxonomies are non-hierarchical, so the value is set to false.
I think it’s a pretty good rule of thumb to use hierarchical taxonomies when you will be using a set number of terms which won’t change too much over time.
I don’t want to go into a lot of details since rewriting rules can cause more trouble than they are worth. However, in some cases you may need to tweak them, especially if you are using generic taxonomy names. If you would like to use the taxonomy name “Car” but you would like the URL to use something else, such as “car-model” you can modify the rewrite rules like so:
Putting It All Together
A full example would consist of all our labels and a number of other options set as needed. Here’s an example I would use in real life if I were implementing our “Athletes” taxonomy:
During the course of this article we looked at the differences between categories, tags, taxonomies and terms and we learned how to easily create our own. Hopefully you now know when to create taxonomies and how to do it.
If you need a nice little shortcut, take a look at this taxonomy generator, which allows you to fill out a form that generates the required functions for you.
If you have any questions about taxonomies or need help creating one, let me know in the comments below.
Originally published at premium.wpmudev.org.