WordPress Custom Taxonomy | How to create custom taxonomies in WP

[ad_1]

Organizing your content is one of the core features of a content-management system (CMS) like WordPress. As such, WordPress contains “taxonomies” to help you keep your content easy to find for both you 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 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

Up and Running is our complete “learn WordPress development” course. Now in its updated and expanded Third Edition, 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 the WordPress 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. All WordPress sites give posts two taxonomies by which they can be organized: Categories and Tags.

These two ways of marking our posts in WordPress have a lot in common. Both can be applied to posts in a “many-to-many” relationship. That means that one tag can be applied to many posts, one category will contain many posts. This is true of both categories and tags. Some people will tell you to limit the number of categories you give a post, often to one. They’ll also give you the advice to use many tags per post. But strictly speaking this is not a technical difference between the two taxonomies, just a cultural difference.

Flat vs Hierarchical: Seeing How WordPress Stock Taxonomies Differ

But there is an important difference between categories and tags in WordPress. You see, 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 what tags mean. 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.” This allows your WordPress system of categories to convey and display extra meaning.

To come back to dogs, you may know about biologists’ 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 instead 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. I personally default to the Pods plugin as the way to go when making custom taxonomies in WordPress without code. But there are many other options.

The advantage to using a plugin like Pods for your custom taxonomy 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 using a plugin can be quicker and never requires you to write PHP, which can be intimidating.

Creating Your Custom Taxonomy with register_taxonomy()

To create a custom taxonomy with your own custom PHP 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 make 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 WordPress.org Developer documentation 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 we’re 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 );
}

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 say 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

[ad_2]

Source link