How to Create Custom Taxonomies in WordPress (2024 Guide)
Do you want to organize your WordPress content in new and powerful ways? By default, WordPress provides two taxonomies – categories and tags – for sorting your posts. But with custom taxonomies, you can extend this functionality to perfectly fit your site‘s unique content.
In this comprehensive guide, we‘ll explain what custom taxonomies are and when to use them. Then we‘ll show you step-by-step how to create your own taxonomies in WordPress, both with a plugin and by manually adding code. Finally, we‘ll cover some more advanced applications and best practices.
Let‘s get started!
What are WordPress Taxonomies?
A taxonomy is a system for classifying and organizing things. In WordPress, taxonomies are used to group related posts together based on shared characteristics.
WordPress ships with two default taxonomies:
- Categories – Best for broad groupings of posts. Categories can be hierarchical, with parent and child levels.
- Tags – Used for more specific keywords and labels. Tags are flat, without hierarchy.
For a blog or news site, categories and tags are usually sufficient for organizing content. But for other types of websites, the default taxonomies can be too limiting. That‘s where custom taxonomies come in.
When to Use Custom Taxonomies
Custom taxonomies allow you to extend WordPress to accommodate your content. Some examples of where custom taxonomies are useful:
- A recipe site could have taxonomies for ingredients, meal type, cuisine, cooking method, etc. This would allow visitors to filter recipes in many different ways.
- An automotive site reviewing cars could categorize vehicles by taxonomies like make, model, class, engine type, etc.
- An encyclopedia or wiki could use custom taxonomies to implement their own classification schemes.
- An ecommerce store could use taxonomies for product categories, tags, brands, colors, sizes, etc.
The possibilities are endless. Custom taxonomies provide the flexibility to build the exact organization system your site needs.
Creating Custom Taxonomies With a Plugin
The easiest way to create custom taxonomies in WordPress is by using a plugin. While there are a few options, we recommend the free Custom Post Type UI plugin. It provides a user-friendly interface for creating taxonomies without having to touch any code.
To get started, install and activate the plugin from the WordPress Plugin Directory. Then navigate to CPT UI > Add/Edit Taxonomies in your WordPress admin panel.
Step 1: Create a New Taxonomy
In the "Add New Taxonomy" section, start by entering a Slug for your taxonomy. This will be used in the URL for the taxonomy archive, so make it lowercase and URL-friendly. For example: "recipes".
Next, enter the Plural and Singular names for your taxonomy. These labels will be displayed in the WordPress admin. For our recipe example, you might use "Recipe Types" and "Recipe Type".
Then click the "Populate additional labels based on chosen labels" link to automatically fill in the other label fields. You can tweak these if needed.
Step 2: Configure Your Taxonomy‘s Settings
Scroll down to the Settings section to configure the key attributes for your taxonomy:
- Hierarchical – Set to True for a category-style taxonomy that supports parent/child relationships, or False for a flat tag-style taxonomy.
- Show UI – Set to True to add the taxonomy to the WordPress admin screens.
- Show in REST API – Set to True to make the taxonomy available in the REST API. This is important if you plan to use the newer Gutenberg block editor.
There are several other settings as well, which are explained inline. In most cases the defaults are fine.
When you‘re finished, click "Add Taxonomy" to register your new taxonomy. You‘ll see it show up in the "Taxonomies" list.
Step 3: Assign the Taxonomy to Post Types
By default, your new taxonomy is not connected to any post types. To use it, you‘ll need to assign it.
Switch over to the CPT UI > Add/Edit Post Types screen. Click on the post type that you want to use the taxonomy with.
Scroll down to the "Taxonomies" section and check the box next to your new taxonomy. Then click "Save Post Type" at the bottom.
Your custom taxonomy is now ready to use! When editing a post in that post type, you‘ll see a new meta box to select taxonomy terms.
Manually Creating Custom Taxonomies With Code
For more control and customization over your taxonomies, you can create them in code. This involves adding a snippet to your theme‘s functions.php file or a custom plugin.
Before editing any core files, it‘s important to set up a child theme. That way your customizations won‘t get overwritten when you update your theme. See our guide on how to create a WordPress child theme.
Here‘s the basic code pattern for registering a new taxonomy:
function register_my_taxonomy() {
$labels = array(
‘name‘ => _x( ‘Taxonomies‘, ‘taxonomy general name‘ ),
‘singular_name‘ => _x( ‘Taxonomy‘, ‘taxonomy singular name‘ ),
// more labels here
);
$args = array(
‘labels‘ => $labels,
‘hierarchical‘ => true,
‘public‘ => true,
‘show_ui‘ => true,
‘show_in_rest‘ => true,
// more args here
);
register_taxonomy( ‘my_taxonomy‘, array( ‘post‘ ), $args );
}
add_action( ‘init‘, ‘register_my_taxonomy‘ );Let‘s break this down piece by piece:
We create a new function where we‘ll register our taxonomy. You can name this anything, but it‘s best to use a descriptive name.
Inside that function, we define an array of $labels for the various user-facing names of the taxonomy. The two most important ones are ‘name‘ for the general plural name and ‘singular_name‘.
Next we set up an $args array to hold the configuration arguments for the taxonomy. The most important ones:
- hierarchical – Set to true for a category-style taxonomy, false for a tag-style one.
- public – Set to true to make the taxonomy visible on the front-end of the site.
- show_ui – Set to true to add the taxonomy to the admin screens.
- show_in_rest – Set to true to include the taxonomy in the REST API.
We then call the built-in register_taxonomy() function, passing in three parameters:
- The name of our taxonomy. Use something short and descriptive, without capital letters or spaces.
- An array of the post type(s) the taxonomy is associated with. For a global taxonomy, you can use simply ‘post‘.
- The $args array holding our taxonomy configuration.
Finally, we hook our function to the ‘init‘ action so that it runs when WordPress initializes. This ensures our taxonomy is registered early.
Once you‘ve added this code and refreshed your site, you‘ll see the new taxonomy show up in the WordPress admin. You can now add terms and assign them to posts.
There are dozens of additional arguments you can use to customize your taxonomy. Check out the register_taxonomy() codex page for a full list.
Displaying Custom Taxonomies in Your Theme
WordPress automatically creates archive pages for your custom taxonomy, using the slug you specified. For example, if your taxonomy is called "recipes", all posts tagged "mexican" will show up at a URL like https://yoursite.com/recipes/mexican.
However, these built-in archives are quite basic. To customize them, you‘ll need to create a taxonomy template file in your theme.
The template name should follow the pattern taxonomy-{taxonomy}.php. So for our "recipes" example, you would create a file called taxonomy-recipes.php.
Inside this file, you can use the standard Loop to display the posts in the term. Here‘s a basic example:
<?php get_header(); ?>
<div id="content">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<h2><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h2>
<?php the_excerpt(); ?>
<?php endwhile; endif; ?>
</div>
<?php get_sidebar(); ?>
<?php get_footer(); ?> This will display a title with the current term name, followed by a list of post titles and excerpts. You can customize this layout however you like.
Advanced Custom Taxonomy Usage
Custom taxonomies become even more powerful when you combine them with WordPress‘s querying and templating systems. Here are a few examples of what you can do:
- Create custom queries to fetch posts by taxonomy terms, using the tax_query parameter of WP_Query.
- Add custom fields to your taxonomy terms using the add_term_meta() function. This lets you store additional metadata like images, descriptions, etc.
- Create custom RSS feeds for your taxonomy terms, allowing users to subscribe to just the topics they‘re interested in.
- Use the wp_dropdown_categories() function to add a dropdown menu of taxonomy terms for filtering posts.
The key is to think of taxonomies as an additional axis to slice and dice your content. By getting creative with queries and templates, you can build highly engaging interfaces for navigating your site.
Best Practices for Custom Taxonomies
Before we wrap up, here are a few tips to keep in mind when working with custom taxonomies:
- Assign clear, descriptive names for your taxonomies and terms. Think about what will make sense to your users.
- Don‘t go overboard. While it‘s tempting to create dozens of taxonomies, too many can become overwhelming. Stick to the most important axes for organizing your content.
- Use hierarchical taxonomies sparingly. Nested folders-within-folders sounds great in theory, but can get unwieldy fast. In most cases, flat tag-style taxonomies are more user-friendly.
- Take advantage of the "Update Counts" feature when registering your taxonomy. This will store the number of posts in each term, optimizing performance.
- If you‘re adding taxonomies to an existing site, consider using a plugin like Term Management Tools to bulk-add terms from your legacy categories or tags.
With a little planning and some creativity, custom taxonomies can take your WordPress site‘s organization to the next level. Have any questions about taxonomies? Leave a comment below!
