WordPress 3 introduced custom taxonomies as a core feature. The following release of 3.1 included many features to enhance support of custom taxonomies. Better import and export handling, advanced queries with
, hierarchical support, body classes and a bunch of wonderful functions to play with were all part of the package.
Let’s take an in-depth look at how to create your own custom taxonomies in WordPress, including a few advanced development examples that you can begin using in your WordPress themes and plugins today.
Taxonomies In WordPress
WordPress’ custom taxonomies make it possible to structure large amounts of content in a logical, well-organized way. In WordPress, categories are set up as a hierarchal taxonomy, and tags are set up as a multifaceted taxonomy.
WordPress offers four built-in taxonomies out of the box:
- Categories (hierarchal),
- Tags (multifaceted),
- Links (multifaceted),
- Navigation menu (hierarchal).
WordPress provides a new method of grouping content by allowing you to create your own custom taxonomies. The core developers have created the
function to handle the heavy lifting for us. All you have to do is understand how to configure all of the settings to suit your needs.
A Practical Example: Content By Location
A business that operates in multiple locations could benefit from organizing its content by location to allow visitors to browse news in their locality. A large news organization could organize its content by world region (Africa, Asia, Europe, Latin America, Middle East, US & Canada), as the BBC does in its “World” section.
Create a Custom Taxonomy
In WordPress, you can create (or “register”) a new taxonomy by using the
function. Each taxonomy option is documented in detail in the WordPress Codex.
After adding this to your theme’s
file, you should see a new taxonomy under the “Posts” menu in the admin sidebar. It works just like categories but is separate and independent.
After adding a few terms to your new taxonomy, you can begin to organize the content in your posts by location. A new “Locations” box will appear to the right of your posts in the WordPress admin area. Use this the way you would categories.
Let’s use this “location” taxonomy as a jumping-off point to learn more about working with taxonomy functions and content.
Create a Taxonomy Template for Your Theme
When you add a custom taxonomy to a WordPress theme, you can display its content using one of WordPress’ taxonomy theme templates.
We could use this to create a theme template for a particular location, such as
taxonomy-location-boston.phpfor the term “boston.”
If the taxonomy were
location, WordPress would look for
This template is used for all custom taxonomies.
If no taxonomy-specific template is found, then the taxonomy that lists pages will use the archive template.
If no other template is found, then this will be used.
to display our content. The template file could look something like this:
(Normally, we would load a template part for the loop, but for the sake of simplicity, I’ve skipped that step. As an alternative to
, we could use
term_description() on this taxonomy archive template to display or retrieve the title and description of the taxonomy term.)
In this example, we’ve used a function called
to retrieve all of the data associated with a taxonomy term in the form of an object. The object returned by the
get_term_by() function contains the following details about the term:
If you need to know the latest news in Boston, then look no further.
0 (or the ID)
1 (i.e. the number of posts with this term selected)
We’ve used this object, then, to display information about the current term
Using Taxonomy Conditionals
Conditional tags can be used in WordPress to determine what content is displayed on a particular page depending on the conditions met by the page. Taxonomy templates have their own set of conditionals:
When any taxonomy archive page is being displayed.
is_tax( 'location' )
When a taxonomy archive page for the “location” taxonomy is being displayed.
is_tax( 'location', 'boston')
When the archive page for the “location” taxonomy with the slug of “boston” is being displayed.
is_tax( 'location', array( 'boston', 'new-york', 'philadelphia' ) )
truewhen the “location” taxonomy archive being displayed has a slug of either “boston,” “new-york” or “philadelphia.”
When a particular taxonomy is registered via
Working With Taxonomy Functions
Many functions for working with taxonomies are available in WordPress. Let’s go through a few commons examples of how to use them in practice.
Display a List of Taxonomy Terms
Most navigation systems begin with an unordered list. You can generate an unordered list of links to taxonomy archive pages using the
function. This function is very customizable and can handle most of the scenarios that you’ll encounter as a theme developer.
If you encounter a situation that requires a custom structure, I would recommend exploring the Walker class or the
Create a Taxonomy Tag Cloud
A tag cloud provides a great way for users to browse content. The
function makes creating a tag cloud with a custom taxonomy easy.
Let’s use it to display a tag cloud of our location terms:[crayon-54923d607a924145743123/]
Get All Terms in a Taxonomy
You will often need to work with a full list of terms in a taxonomy, and the
function can be quite handy for this. Let’s use it to show off the number of locations to which we’re providing news:
This will output the following HTML:[crayon-54923d607a94e656890055/]
This simple approach may not show it, but the
get_terms() function is incredibly powerful. It allows you to get terms from multiple taxonomies at once by passing an array that contains the names of your taxonomies as the first parameter.
Working With WP_Query and tax_query
WP_Query class enables you to create a custom loop. WordPress 3.1 introduced a new parameter for the class called
tax_query, which allows you to display content from a taxonomy in many unique ways.
Let’s use it to create a list of the most recent news posts in Boston.[crayon-54923d607a962115119384/]
We could easily make this set-up dynamic by using the
functions that we discussed earlier.
Attaching Additional Data to a Taxonomy
Each taxonomy term has specific data associated with it. Out of the box, WordPress allows you to store the following information for each taxonomy term:
But what if you need to store more information, such as an image for the taxonomy term or a title and description for search engines, or maybe even attach the term to a particular author the way a traditional news column does? Since WordPress 2.9, developers have been able to attach additional meta data to posts, pages, custom post types, comments and users using the
functions. But this does not include taxonomies such as tags and categories.
With the help of the Taxonomy Metadata plugin, we can attach meta data to taxonomy terms for both built-in and custom taxonomies. This enables us to create additional taxonomy fields that will be stored in a new
Note to Multisite developers: I’ve encountered issues using the Taxonomy Metadata plugin on a WordPress Multisite installation. Activating the plugin network-wide results in data not being saved. Instead, activate the plugin individually for each website, and it will work properly.
(Knowing the story behind this technique and what it might mean for future WordPress upgrades is important. Currently, there is a debate on the WordPress Trac project about the best method for this. The method I’ll show you is one suggested by various WordPress core developers. But I strongly urge you to review the Trac project and the Codex. A standard approach could very well be built into WordPress in the future, which would therefore be more practical than what I am about to show you.)
The prerequisite to all of the examples below is to install and activate the Taxonomy Metadata plugin.
Add Search Engine Title and Description Fields to Categories and Tags
We will use action hooks to gracefully attach additional fields to our taxonomies without editing WordPress’ core. If you’ve made it this far, then you probably have a working knowledge of WordPress filters and actions. To learn about working with hooks, I highly suggest Daniel Pataki’s article on the subject.
Let’s start by adding a text
field to the “Add New” and “Edit” term pages on the WordPress admin screen. We do this by placing the following functions in our theme or plugin.
taxonomy_metadata_add() function attaches the fields to the
item in the URL above will change depending on the term you are editing.
taxonomy_metadata_edit() function attaches the fields to the
items in the URL above will change depending on the term you are editing.
We’ll use the
function here to display any saved data that exists in the form.
These two functions control the output of the form fields. I’ve used HTML that follows WordPress’ UI patterns and styles guidelines for the admin area.
Saving the form’s data to the
taxonomymeta database table
Now that we’ve added the form fields, we’ll need to process and save the data with the
function that is provided by the plugin.
Add the new taxonomy fields
Now that everything is in place, we’ll use action hooks to load our new functions in all the right places. By hooking the following function into the
action, we ensure that it runs only on the admin side of WordPress. First, we need to make sure that the functions added by the Taxonomy Metadata plugin are available. Next, we use the
function to attach the new taxonomy fields to every public taxonomy, including the built-in tags and categories.
That’s it. We’re done!
You should now see two additional fields in your tags, categories and public custom taxonomies. As mentioned at the beginning of this section, the technique can be used to handle many different scenarios. This basic framework for storing and retrieving information associated with a taxonomy should have you well on your way to mastering the management of taxonomy content.
I hope you better understand how to organize WordPress content with the help of taxonomies. Whether hierarchal or multifaceted, a well-implemented taxonomy will simplify the way content is organized and displayed on a website. WordPress has all of the tools you need to create custom taxonomies and to group your content in new and exciting ways. How you use them is up to you!