Using Categories, Tags and Taxonomies Properly in WordPress

Using Categories, Tags and Taxonomies Properly in WordPress

Categories, tags, taxonomies, and terms in WordPress can be confusing. When you first start out with WordPress, you’ll no doubt happily use categories, and possibly tags, but then as you start to delve into the system more and use it as a CMS, then custom taxonomies will come into play.

But what’s the difference between them all? And how can you use them on your site?

In this post, I’ll attempt to demystify categories, tags, taxonomies and terms, and identify uses for each of them. I’ll give you some examples of how to use them and look at how our own plugins use them to add extra functionality to your site.

Categories, Tags, Taxonomies and Terms: Definitions

Let’s start with some definitions, so that we know exactly what we’re talking about. The first thing to define is a taxonomy, as that’s the broadest of the terms we’re dealing with.

What is a Taxonomy?

Taxonomy isn’t a word that’s specific to WordPress. The Oxford English Dictionary defines taxonomy as:

“A scheme of classification.”

It’s a term often used in science, to classify organisms. But that’s not really very helpful.

The WordPress Codex gives us a more helpful definition:

“In WordPress, a ‘taxonomy’ is a grouping mechanism for some posts (or links or custom post types).”

That’s more specific but doesn’t really pin it down. Maybe it’s easier to define taxonomy by providing some examples.

A taxonomy is a grouping system that you use to classify any kind of post in WordPress. For example category is just one taxonomy. As is tag. You can also create your own custom taxonomies which you apply to posts or to a custom post type. It’s very common to create a custom taxonomy to classify posts in a custom post type, as this avoids any confusion between the custom post types and normal posts.

For example, if you install our Support System plugin, it will register two custom post types for you: Support Ticket and FAQ. It will also register two custom taxonomies: Ticket Category and FAQ Category. This lets you categorize your tickets and your FAQs separately from your blog posts, and from each other.

What is a Term?

A term is the grouping with in a taxonomy that you use to separate out your posts.

So for example if you were using the Support System plugin and you’d created categories for each of your support teams — maybe billing, sales and support — each of those would be a term in the Ticket Category taxonomy. You would set up different terms for the FAQ Category taxonomy, although they may be duplicates.

The Support System plugin in the WordPress admin
The Support System plugin registers two custom post types and two custom taxonomies

But as far as WordPress is concerned these are different terms and will be stored separately in the database. Even if you give them the same names.

What is a Category?

This brings us back to categories, which people frequently confuse with taxonomies. This is how categories work:

  • Category is a taxonomy.
  • An individual category that you add is a term in the category taxonomy.

So if you created categories of recent and featured for your posts, they would be terms in the category taxonomy, just as billing, sales, and support are terms in the ticket categories taxonomy.

So in That Case, What’s a Tag?

A tag is similar to a category. It’s a term in the post_tag taxonomy. So if you have WordPress and tutorials as tags for your posts, they are terms in the post_tag taxonomy.

(Don’t ask me why tags are called post tags and categories aren’t called post categories, I’m afraid it’s one of WordPress’s many mysteries).

But, you might be asking, what’s the difference between a category and a tag? And why do we need them both?

Categories and tags differ in one crucial respect: categories are hierarchical, and tags aren’t. You can create child categories of a parent category but you can’t do the same with tags. This is why you’re much more likely to use categories to structure your site than tags, as they have their own inherent structure.

When you register your own custom taxonomies (or when a plugin does it for you), these can either be hierarchical or not – so they can behave like a category, or like a tag.

Phew! I hope that all makes some sense now.

Creating Taxonomies and Terms

Now that we’ve identified what a taxonomy and a term is, let’s take a look at how you create these.

WordPress has a few built-in taxonomies. These are:

  • Category
  • Tag
  • Link category – these don’t apply to normal posts, but to the link post type. This is a hangover form when WordPress had a feature called the blogroll. You can safely ignore link categories.
  • Post format – these apply to normal posts, and are used to identify what format of post it is, such as video, text etc. I imagine these are useful for bloggers but I haven’t used them myself or seen many examples of them being used – iff you know of a good one, add it to the comments!

So WordPress comes with four default taxonomies out of the box, two of which are particularly useful. The category taxonomy has one term pre-set – the uncategorized term. But the post tag taxonomy doesn’t have any terms until you add your own.

But this needn’t be the limit of the taxonomies and terms on your site. WordPress lets you register your own custom taxonomies and apply them to whatever post type you like. You can register new taxonomies for your posts, or register them along with a new post type that you apply them to. Or both.

You can either register your taxonomy with code using the register_taxonomy() function, or you can use our CustomPress plugin to create new taxonomies without writing any code.

CustomPress plugin on WPMU DEV website
The CustomPress plugin makes registering custom taxonomies easy

Either of those methods will register a taxonomy for you. You can also create terms programmatically, using the wp_insert_term() function. This is useful if you’re writing a plugin with a taxonomy that you want to pre-populate with terms. So if you were writing a support ticket plugin like ours, for example, you might create a priority taxonomy and pre-populate it with the terms high, medium, and low. (Not that anyone would ever use anything other than high!)

But the easiest way to create a term is via the relevant screen in the WordPress admin. Here’s the screen to create a ticket category for our Support System plugin: adding a new term here is just like adding a new category via the Categories screen:

Editing ticket categories in the admin screen

Creative Use of Taxonomies, Categories, and Terms

Most often, categories are kept separate from custom taxonomies and their terms. This makes sense when you’re using a third-party plugin that needs to work on a wide variety of sites – the developer had no way of knowing how you’re using categories or standard posts in your site and so won’t make the taxonomies registered by the plugin interact with these.

But if you’re creating your own plugin, you can be more imaginative.

Using Custom Taxonomies to Add Granularity to Normal Posts

If your site is entirely based on posts, there may come a time when categories aren’t enough to classify them. You may find that you’re using multiple layers of child category and that your top-level categories should really be separate taxonomies.

One way to identify if this is happening is to ask yourself whether there’s any interaction between the different child taxonomies. Also, ask yourself whether you’re ever assigning the top-level categories to your posts.

So if you’ve created a blog all about books and you have author and genre as top-level categories, with subcategories of different authors and genres, then you really should be creating two new taxonomies: author and genre. This is for two reasons:

  • Each post about a book will have just one category under the author assigned to it as well as one (or maybe more than one) under genre, and it will always have both.
  • You will never assign the author or genre top-level categories to a post.

Create new taxonomies also lets you code a custom query that finds all of the books in a particular genre by a particular author. This can be done using categories but isn’t as neat.

Using Custom Taxonomies with Posts as Well as Custom Post Types

There’s no reason why you can’t register a custom taxonomy and have it apply to a custom post type you’ve registered as well as your standard posts.

Let’s say you’re working on your books blog. You’ve created a custom post type called review which you use to list reviews of the books you’ve read. This is separate from your normal blog posts, which will be more generic. You might register a custom taxonomy of genre for your reviews so that your readers can find reviews of books in that genre.

But why not use that taxonomy for your blog posts as well? Then you can write posts about each genre you’re interested in, and use the pre_get_posts hook to list posts and reviews in that genre on the taxonomy term archive page. Or you could create a custom template file for that taxonomy (called taxonomy-genre.php) to run two loops: one for reviews and one for posts.

Adding Taxonomies to Pages

You can also add taxonomies to post types that don’t normally have taxonomies registered to them, such as pages. To add taxonomies to post types, you use the register_taxonomy_for_object_type() function. This lets you add any pre-existing taxonomy to any pre-existing post type. So to add categories to pages, for example, you’d add this to your theme functions file (or to a plugin):

If you want to do this with a taxonomy you’ve registered yourself (rather than a pre-existing one), you simply add this to the code registering your taxonomy, using the register_taxonomy() function. The second parameter of this function is the post type(s) that the taxonomy applies to, and if you want to use more than one post type you include them in an array.

So if you’ve registered a review post type and want to register a genre taxonomy which will apply to reviews, pages, and posts, you register the taxonomy with this:

The first parameter is the taxonomy ID, the second is the array of post types and the third is the arguments for the taxonomy you’ve already defined.

By applying taxonomies to pages as well as your custom post type, you can create styling for reviews, posts, and pages in that genre. You can also take things further by identifying what category a given page is in and then running a custom query in your page template that outputs posts and reviews from that category after the page content. Or you could display the page contents with the reviews and posts on the taxonomy archive page. The sky’s the limit!

Taxonomies, Categories, Tags, and Terms Can Be Very Powerful

The ability to register your own taxonomies is one of the things that turned WordPress into a content management system instead of a simple blogging platform. By making use of all of the methods of classifying your content that is available, you can create bespoke sites that display exactly what content you want them to, where you want them to.

Just don’t get caught up with the difference between taxonomies and categories: category is just one example of a taxonomy, and a category you add to your site is a term in the category taxonomy. Remember that, and you’ll easily get the hang of categories and taxonomies.

Have you extended the functionality of categories, tags and taxonomies on your site, or do you have ideas for doing so? Share your thoughts in the comments below.
Rachel McCollin
Rachel McCollin Rachel is a freelance web designer and writer specializing in mobile and responsive WordPress development. She's the author of four WordPress books including WordPress Pushing the Limits, published by Wiley.