Let's say that you've recently started developing things with WordPress. Perhaps you've created basic theme or a plugin that does something simple, but you're ready to jump into more complex topics.
This may include things such as custom meta boxes, the Settings API, custom post types, or using custom taxonomies. All of these are valuable things to learn and they each afford their own unique functionality within the overall context of WordPress.
Some of these topics we've covered in detail before, some of them we've not. One of the topics that tends to give developers who are just starting out with WordPress development a little bit more trouble than others is the concept of taxonomies.
In this series, we're going to be taking a beginner's look at taxonomies - from the concept to how they work in WordPress and from how we can use them in themes and plugins to implementing them in our own plugin.
If you're an intermediate to experienced WordPress developer, then this series may not be suited for you; however, if you're a budding WordPress developer or someone who is looking to grow your expertise in an area that you've not previously worked with, then feel free to follow along with the rest of the series.
In order to start with the broadest possible idea of taxonomies, let's take a look the definition. According to Wikipedia:
Taxonomy is the practice and science of classification.
It sounds simple, right? In short, it's a way to classify things. But if that's the case, then why does the topic cause us trouble when working with WordPress?
According to the WordPress Codex:
Taxonomy is one of those words that most people never hear or use. Basically, a taxonomy is a way to group things together.
And this definition isn't really altogether much different from what Wikipedia provided, is it? The common denominator between both of these definitions is that it's a way to group things together.
That's it. That's the basic definition of taxonomy and that's what we need to understand in order to build out a working understanding of how they work within WordPress.
Where Are They Used?
Taxonomies are used throughout WordPress most prominently in two different ways:
Every base installation ships with these particular features for the standard post type, and many of us use them on a daily basis either when working with our own blog, managing someone else's blog, or perhaps building a site for someone using WordPress.
If you're brand new to WordPress, then you should know the following:
- A category taxonomy allows you to group posts together in under a common category. For example, you may grow some posts under Videos and other posts under Photographs.
- A tag taxonomy allows us to similarly classify our content, but in a slightly less structured way. For example, under Videos, you may tag your videos using tags that are related to the frame rate of the video. This wouldn't make sense as a category because photographs don't have frame rates.
You can think of categories as being more structured and tags as being more free form (or simply less structured), but that still raises questions as to how they fit within WordPress and how we can actually go about creating some type of conceptual model for taxonomies.
How Do We Classify Them?
It's helpful to think of taxonomies in two forms: Hierarchical and Non-Hierarchical.
The most common example of a hierarchical taxonomy is a category. A hierarchical taxonomy is one that can exist a single taxonomy on its own, or it can have children, grand-children, and great-grand-children elements.
The basic hierarchical - or category - taxonomy that ships with WordPress is Uncategorized. But let's say you were building out a media site with photos and videos. From here, you may have two categories: Photographs and Videos.
Then, under Photographs you may have Outdoor, Lifestyle, Families, Children, and/or Weddings. For your videos, you may have High-Definition, People, Wildlife, Film, and so on.
If you were to see this in the WordPress dashboard, it may look something like this:
Of course, you could have each of the child categories have their own children. For example, the Videos High-Definition could have 720p and 1080p. As you can see, hierarchical taxonomies work best when there is a clear way that you're going to be organizing your information.
Sometimes, a good way to think of a real world example of a hierarchical taxonomy is through the use of a filing cabinet with drawers and then folders that fit inside each of the drawers. In our case, Photographs would be a filing cabinet, Outdoor, Lifestyle, etc. would be the drawers. If you were to have another level of categorization, those could be folders inside the drawer.
But what about non-hierarchical taxonomies? The most common example of these is a tag. A non-hierarchical taxonomy is one that exists all on its own. There's no notion of children and they can be added easily and freely to posts (and may co-exist with categories).
Building on our previous example, let's say that your Photographs and your Videos are divided into Black and White or Color. Though these could work as categories, they also work as tags in that they can apply to both photographs and videos.
Just as we think about categories as a filing cabinet with drawers, sometimes it's helpful to think of tags as stamps or labels - an way in which an existing item, category, or even sub-category is identified with similar content.
Say that we have our filing cabinet - one cabinet for Photographs and one cabinet for Videos. Then inside each of those cabinets are drawers. For the former, we have drawers for Outdoor, Lifestyle, Families, and so on, and for the latter we have High-Definition, People, Wildlife, and so on.
Then, let's say that each item in those drawers are identified as either Color or Black and White. You may also want to stamp each photo with a season, a year, a location, and so on. Anyway, each item would have perhaps a label, a stamp, a post-it note, or a tab to indicate the type of film that was used to shoot the subject matter.
From a technical perspective, this gives us the ability to pull back all Videos and Photographs that are Colored as well as allows us to pull back just Wildlife videos that are in Black and White.
The combinations that you can use to retrieve this information gets more and more complex - and powerful - based on the amount of information that you have classified.
With this introduction in place, we're ready to turn our attention to a more technically focused approach to working with taxonomies. That is, we're ready to look at taxonomies within the context of themes and plugins, when it's appropriate to use which, where, and why, and how compatibility factors into the conversation.
After that, we'll look at implementing the taxonomies that we've outlined in this article via the WordPress API into a working example that you can download and install in your local version of WordPress in order to read the code, tinker with the code, and review the code to give you a concrete understanding of how to use the code in your work moving forward.