WordPress Custom Taxonomies: How and Why to Create Them

Organizing your content is one of the core features of WordPress content-management system like WordPress. As such, WordPress creates what are called “taxonomies” to help you keep your content easy to find for both your and your visitors. Today we’re focused on why and how to make a WordPress custom taxonomy.

Even if the term “taxonomy” is new, WordPress’s two main default taxonomies, “categories” and “tags,” should be familiar. WordPress custom taxonomies are similar to these default taxonomies in that they organize things—the difference is that they can be anything you choose, from “Difficulty” to “Product Type.” We’ll start this article by explaining a bit more about taxonomies, and then we’ll get detailed about how and why to create your own WordPress custom taxonomies.

Before we dive in, an invitation. If you’re looking to more deeply understand WordPress’s data structures—and WordPress development in general—we’ve written the best guide out there:

The Best Way to Learn WordPress Development

Get Up and Running Today!

Up and Running is our complete “learn WordPress development” course. Now in its updated and expanded Third Edition for 2018, it’s helped hundreds of happy buyers learn WordPress development the fast, smart, and thorough way.

Here’s what they have to say:

“I think anyone interested in learning WordPress development NEEDS this course. Watching the videos was like a bunch of lights being turned on.” -Jason, WordPress developer

“Other courses I’ve tried nearly always lack clear explanations for why WordPress does things a certain way, or how things work together. Up and Running does all of this, and everything is explained clearly and in easy-to-understand language.” -Caroline, WordPress freelancer

Understanding Taxonomies in a CMS

A taxonomy is simply a system of organizing information. A WordPress taxonomy, specifically, organizes WordPress posts.

If you’re using WordPress as a blog, you’re already using taxonomies—you just may not know them by that name. A taxonomy is simply a system of organizing information. A WordPress taxonomy, specifically, organizes WordPress posts. Your WordPress posts have two taxonomies by which they can be organized: Categories and Tags.

Tags are considered a flat taxonomy: all tags are equal, and no tags are members of other tags. This makes it great for quick entry of possibly-relevant data, but makes it hard to be organized and disciplined about it. You might think to tag a dog with things like “brown”, “furry”, “soft”, “cuddly”, and “cute”. All of these apply, but they don’t all fit into a single way of thinking about dogs.

In contrast, categories are a hierarchical taxonomy: elements can be nested such that something in your “Five Paragraph Essays” category is automatically also a member of its parent category “Essays.”

Or to come back to dogs, we know our way of understanding all living Dog analogy of How do I create a custom taxonomy in WordPress? things as a hierarchical taxonomy. Domestic dogs are often identified as Canis lupis (familiaris). Canis is the genus, lupis is the species. All species lupis are members of the taxonomical category of Canus, but the reverse is not true. Coyotes also belong to the Canus genus, but are not either dogs (or wolves).

All living creatures are put into the taxonomical system of scientific classification. If you learned “kingdom-phylum-class-order-family-genus-species” in a biology class ever, that’s what we’re talking about here. The scientific classification system is a hierarchical taxonomy, just like categories. It’s unlikely your WordPress content needs as extensive an organization system, but it might. 😉

Why Make a Custom Taxonomy in WordPress?

A custom taxonomy is a custom organizing system that you create, which could be either flat or hierarchical.

So we know that tags and categories are taxonomies, and we know that tags are flat and categories are hierarchical. A custom taxonomy is a custom organizing system—either flat or hierarchical—that you create for your posts.

You would create additional taxonomies when you think they’ll be useful to either you or your readers. For example, on WPShout we think that some of our readers may want to see only “Beginner” (like this) or more advanced content, so we created a new taxonomy called difficulty. But this doesn’t make sense on every, or even most, sites, so it’s not rolled into WordPress by default.

Other examples: if you have a travel blog, you might use a flat (tag-like) custom taxonomy called “Country,” which captures which country (or countries) you were in when you wrote each post. This way they wouldn’t be mixed into your use of WordPress’s ordinary tagging system to tag your posts with, say, “Local cuisine” and “Major landmarks,” while also letting you define, separately, whether a given post was written while you were passing through Italy and Slovenia.

A film blog might want a hierarchical (category-like) system called “Genre,” which captures that a given movie is, say, a “Comedy,” and possibly also a member of the “Romantic Comedy” subcategory. You could use this system separately from WordPress’s default “Categories,” which might capture, say, whether a given article was a “Movie Review” or just a “Detailed Plot Summary.”

In all cases, when registering a taxonomy you want to just ask yourself if it will be necessary or helpful for the sites ongoing readers and maintainers. If it’s useful to any, it’s probably worth making. But be realistic, it’s so quick to make new taxonomies in WordPress that you want to keep in mind if you (or other on-going site maintainers) will actually use them for the whole duration of the site. There isn’t a right answer, but too many taxonomies probably just amounts to a bunch of unnecessary interface clutter.

Using a Plugin to Create a Custom Taxonomy

Just as we talked about with custom post types, you can create custom taxonomies in two basic ways: with an existing plugin, or by writing a custom plugin.

The third-party plugins that allow you to create custom post types and custom taxonomies are mostly the same as for custom post types, and work well in general. For those unwilling to follow the link (I get it), I personally generally think of the Pods plugin as the way to go on this. But there are many other options.

The advantage to using a plugin like Pods is that it can be both quicker and easier than doing the registration of the taxonomies in code yourself. Fundamentally, you will have to decide on the name and organizational characteristics of your new custom taxonomy in either case, but plugin can be quicker and never require you to write PHP, which can be intimidating.

Creating Your Custom Taxonomy with register_taxonomy()

To create a custom taxonomy with your own custom plugin, you use the WordPress function register_taxonomy, which has two required arguments:

  1. The slug name of your custom taxonomy. “Slugging” is the same process of working with text that helps make WordPress post titles into URLs. “Slugged” text looks like this: “i-am-slugged-text”. So for a taxonomy called “Movie Genre,” the “slug name” would be movie-genre.
  2. The post types that you want the taxonomy to apply to. This is also “sluggified”, so if you want the taxonomy to apply to the WordPress default post type of “Page”, you’d made the second argument 'page'.

This second argument can also be an array, which would be a list of post types. If you want a single post type to get the taxonomy, say post that’s all you need. But if you wanted both posts and pages to have it, you’d make the second argument array('post', 'page').

To make this very concrete, if your taxonomy were wanting to apply to Posts, Pages, and a new post type you made called “Awesome”, you’d use register_taxonomy() as follows:

add_action('init', 'wpshout_register_taxonomy');
function wpshout_register_taxonomy() {
    register_taxonomy( 'awesome_taxonomy', array( 'post', 'page', 'awesome' ) );
}

Note that our call to register_taxonomy() is wrapped in another function, which hooks into the init action hook. If this style of function writing is new to you, please read our introduction to WordPress hooks, which is an absolutely key piece of WordPress knowledge.

Customizing Taxonomy Options with $args

register_taxonomy() does accept an optional third parameter: an array of arguments, often (by convention) saved to a variable named $args.

The Codex specifies the various arguments you can pass in, but in our examples here I’m going only specify a value for two. This lets me keep my code very compact, but it can lead to some sub-optimal labels for interface buttons. This is trade-off that you have to weigh for yourself. Personally, I dislike but often honor WordPress’s need for me to write each label out for it because I like the interface to look a little more polished.

Here were only going to specify a 'label', which is the text string (like “Tag,” “Category,” or “Genre”) that names the taxonomy for users. In our example case, our label is 'Awesome Taxonomy'. We’ll also specify a true or false value for 'hierarchical'. The default is false, which makes your taxonomy “flat” or “tag-like”, but for clarity I like to specify false even if the value should just be the default.

With these two arguments specified, our final registration function for our example would look like:

add_action('init', 'wpshout_register_taxonomy');
function wpshout_register_taxonomy() {
    $args = array( 
        'hierarchical' => true,
        'label' => 'Awesome Taxonomy',
    );
    register_taxonomy( 'awesome_taxonomy', array( 'post', 'page', 'awesome' ), $args );
}

A Quick Note About Taxonomy Metadata

In WordPress, taxonomies have been there almost since the beginning. But often developers found that they wanted the ability to hold and store some other data related to a taxonomy. That wasn’t really possible for a while, but now it is. This is not — by a long stretch — a common use case for taxonomies. If you’re using your taxonomy from a plugin you really don’t need to think about this feature. But because WordPress has the feature and it’s now possible, I just want to highlight it very briefly

Since WordPress 4.4 (in December 2015) you’ve been able to add metadata to taxonomy terms. A common, and likely, use case for this if if you wanted to add icons or photos to your Categories. Before term meta, there were hacks for this, but not it’s possible to use term meta itself to add this. We won’t highlight that here, but instead we’ll point you this great article on the topic from Justin Tadlock. Our core goal is to make sure you know it’s possible to store custom data on taxonomy terms, if you need it.

Start Registering WordPress Custom Taxonomies!

Custom taxonomies are one of the features that really makes WordPress a fully capable CMS. Taxomonical data storage make organizing your content in sane ways so much easier. Not every WordPress site needs to have a custom plugin for new taxonomies — I’d even sat that most don’t. But when you need them, it’s great to know they’re so easy to add into a site. Organized data can be reused for years to come.

Image credit: evelynishere


7 Responses

Comments

  • “Since WordPress 4.4 (in December 2015) you’ve been able to add metadata to taxonomy terms. A common, and likely, use case for this if if you wanted to add icons or photos to your Categories.”

    Thanks! I was looking into doing this very thing just yesterday, wanting to add an image to each term in a custom taxonomy (created by a plugin). I didn’t think it was possible, so instead I was thinking of creating a custom post type (to hold the images) with a post corresponding to each term in the taxonomy. I’m glad to find there’s a neater way to do it.

  • Miguel says:

    When registering custom taxonomies with a custom plugin, it is better to hook it into “init” , or using the plugin activation hook? I always thought it was better to use the plugin activation hook, otherwise you are unnecessarily querying the DB every time the “init” action is fired, isn’t?

Pingbacks