WPLift is supported by its audience. When you purchase through links on our site, we may earn an affiliate commission.

How to Create WordPress Custom Taxonomies (2021)

Last Updated on September 8th, 2021

Published on July 20th, 2020

Tags: ,

Share This Article

WordPress comes with two taxonomies built-in: categories and tags. While those are sufficient for blogs and basic websites, the time will most likely come when you may want to add your own. Thankfully, creating WordPress custom taxonomies to give you greater flexibility and control over your data is relatively straightforward.

What are WordPress Custom Taxonomies?

If you are not entirely sure what a taxonomy is, Dataversity has a beautifully simple definition:

Taxonomy represents the formal structure of classes or types of objects within a domain. It organizes knowledge by using a controlled vocabulary to make it easier to find related information.

So, in a nutshell, taxonomies are just a way of structuring, organizing, and identifying data.

WordPress essentially began as a blogging platform, so its default taxonomies cater to those kinds of sites. However, quickly realizing that people were using the platform for other types of websites and not just blogs, version 3.1 of the platform introduced WordPress custom taxonomy functionality. This convenient feature enables you to organize your site and structure your data precisely to suit your needs.

Great examples of custom taxonomies include news websites such as CNN or BBC, where the content needs arranging geographically or by topic.

Taxonomies can be ‘hierarchical,’ as is the case with categories with parent-child relationships or ‘multi-faceted’ like tags. Hierarchical taxonomies are best suited to things where the relationship is well known, rather like a family tree. In contrast, faceted taxonomies are used where it is possible to apply multiple similar values to dissimilar items. Thus, a faceted one would probably be a better option when creating your taxonomies if you have items duplicated in several places.

No doubt you are now asking yourself, “How do I create a custom taxonomy in WordPress?” There are a couple of ways: manually with code or using a dedicated WordPress custom taxonomy plugin. We will be guiding you through both methods.

How to Create WordPress Taxonomies Manually with Code

We know that the word ‘code’ sends shivers down the spines of many people, but honestly, it’s not rocket science, and using code as opposed to plugins keeps the ‘weight’ of your site down considerably.

We will now walk you step-by-step through the process of coding your own custom taxonomies:

Article Continues Below

1. Establish what kind of taxonomy you need

Deciding whether or not your taxonomy needs to be hierarchical or non-hierarchical is necessary as each requires a different code.

As with categories, a hierarchical taxonomy allows you to have parent and child terms. So, for example, your parent term might literally be ‘Children,’ and below that would be child terms like ‘Newborn,’ ‘Infants,’ ‘Junior,’ and ‘Adolescents.’

Non-Hierarchical, or multi-faceted taxonomies, on the other hand, are essentially like tags in that they only allow you to define one term to assign to content, and you cannot create any parent-child relationships.

2. Login to WordPress and go to the theme editor

We strongly recommend you make a backup of your site before doing any manual changes, as any errors you make in the PHP code will cause significant issues. Further information on how to create a backup is available here.

Log into your site’s WordPress admin panel in the usual way. Once you are in, scroll down and hover your cursor over ‘Appearance.’

Click on ‘Theme Editor’ in the dropdown box that appears:

Create WordPress custom taxonomies with code

A warning message advising you not to tamper with your theme’s backend may appear, in which case you can click on ‘I understand’:

WordPress custom taxonomies with code

However, if editing your site from the backend does not sit comfortably with you, then we strongly suggest you hit ‘Go back’ and use a plugin to create your custom taxonomies instead

3. Locate the Theme Functions

Navigate to the far right-hand side of the screen. Under ‘Theme Files,’ locate and click on ‘Theme Functions (functions.php)’:

Create WordPress custom taxonomies with code

Code will now appear, and at the very top of that, you should see <?php:

Article Continues Below

4. Insert the new code

Locate your cursor at the end of this first line. Press enter to add a new line and copy/paste either of the following blocks of code into that new line as applicable:

For Hierarchical Taxonomy

//firstly hook into init action and call create_england_taxonomies when it fires
add_action( 'init', 'create_counties_hierarchical_taxonomy', 0 );
//next create a custom taxonomy name it counties for your posts
function create_counties_hierarchical_taxonomy() {
// add your new taxonomy and make it hierarchical
//firstly do the translations part for GUI
  $labels = array(
    'name' => _x( 'Counties', 'taxonomy general name' ),
    'singular_name' => _x( 'Counties', 'taxonomy singular name' ),
    'search_items' =>  __( 'Search Counties' ),
    'all_items' => __( 'All Counties' ),
    'parent_item' => __( 'Parent County' ),
    'parent_item_colon' => __( 'Parent County:' ),
    'edit_item' => __( 'Edit County' ),
    'update_item' => __( 'Update County' ),
    'add_new_item' => __( 'Add New County' ),
    'new_item_name' => __( 'New County Name' ),
    'menu_name' => __( 'Counties' ),
  );   
// register the taxonomy
  register_taxonomy('counties',array('england'), array(
    'hierarchical' => true,
    'labels' => $labels,
    'show_ui' => true,
    'show_in_rest' => true,
    'show_admin_column' => true,
    'query_var' => true,
    'rewrite' => array( 'slug' => 'county' ),
  ));
}

For Non-Hierarchical Taxonomy

//firstly hook into init action and call create_towns_nonhierarchical_taxonomy when it fires
add_action( 'init', 'create_towns_nonhierarchical_taxonomy', 0 );
function create_towns_nonhierarchical_taxonomy() {
// labels part for the GUI
  $labels = array(
    'name' => _x( 'Towns', 'taxonomy general name' ),
    'singular_name' => _x( 'Towns', 'taxonomy singular name' ),
    'search_items' =>  __( 'Search Towns' ),
    'popular_items' => __( 'Popular Towns' ),
    'all_items' => __( 'All Towns' ),
    'parent_item' => null,
    'parent_item_colon' => null,
    'edit_item' => __( 'Edit Town' ),
    'update_item' => __( 'Update Town' ),
    'add_new_item' => __( 'Add New Town' ),
    'new_item_name' => __( 'New Town Name' ),
    'separate_items_with_commas' => __( 'Separate towns with commas' ),
    'add_or_remove_items' => __( 'Add or remove towns' ),
    'choose_from_most_used' => __( 'Choose from the most used towns' ),
    'menu_name' => __( 'Towns' ),
  );
// register the non-hierarchical taxonomy
  register_taxonomy('towns','england',array(
    'hierarchical' => false,
    'labels' => $labels,
    'show_ui' => true,
    'show_in_rest' => true,
    'show_admin_column' => true,
    'update_count_callback' => '_update_post_term_count',
    'query_var' => true,
    'rewrite' => array( 'slug' => 'town' ),
  ));
}

In both the above cases, you will need to substitute the taxonomy name, labels, and post types with the actual ones you wish to use.

Your screen will now hopefully look something like this, in which case you can press the ‘Update File” button to save the code.

Displaying WordPress Custom Taxonomies

Even though you have now added custom taxonomies to your WordPress using code, you now need to configure where to show them.

1. Establish where you want to display your custom taxonomy

To do this, you will need to get familiar with the template hierarchy in WordPress as we are going to add some code templates into the theme or child theme where your custom taxonomies are to appear. In most circumstances, it will only be necessary to add code to the single.php, content.php, or possibly another file within the template-parts folder of your theme. However, it is also theoretically feasible to add a custom taxonomy to any template file.

Suppose you are really unsure as to how to find the template used by a specific page. In that case, we suggest using Which Template Am I plugin. That will show the name of the template in use for the current page, and you can safely deactivate the plugin once you have pinpointed the template you need to work with.

2. Add the code to where your custom taxonomy is to show

Once you have established where you want to display the new taxonomy, locate the corresponding .php file. These can usually be found in the same way as you located the functions.php file by going into the WordPress Theme Editor and selecting the appropriate file under the ‘Theme Files’ list. Once you have found the correct .php file, paste the following code to it:

<?php the_terms( $post->ID, 'towns', 'Towns: ', ', ', ' ' ); ?>

Remember to substitute the elements in red with the taxonomy name you used.

That’s it; you have now added all the necessary code to create and display a custom taxonomy within your WordPress. If your new taxonomy is not showing, ensure that you have added the code to the correct file. If the problem persists, you might wish to reach out to the developer of your particular theme for guidance on customizing the display to include your new taxonomy.

In all instances, the WordPress Codex is the go-to for everything relating to your site’s backend, including custom taxonomies and template hierarchy.

Article Continues Below

How to Create WordPress Taxonomies Using a Plugin

If you are uncomfortable adding code to your WordPress to create custom taxonomies, we strongly suggest using a WordPress custom taxonomy plugin instead.

The main advantage of using a WordPress taxonomy plugin is it simplifies the process, which is ideal for novices, but the downside is plugins can slow your site down. That said, taxonomy plugins are not particularly code-heavy, so you might want to use one for sheer convenience anyway.

Here’s how to create a custom taxonomy using a plugin:

1. Install and activate the plugin

There are several plugins for adding custom taxonomies, but the one we will be using to demonstrate the process is the Custom Post Type UI plugin:

How to Create WordPress Taxonomies Using a Plugin - Install

This popular plugin is available on the WordPress plugin directory. It is entirely free, very lightweight, and easy to use, so go ahead and download and activate it in the usual manner. Other plugins achieve the same thing, and the process of adding taxonomies should be reasonably similar whichever you choose.

2. Go into ‘Add/Edit Taxonomies’

A new item titled ‘CPT UI’ should have appeared in the menu to the left of your WordPress admin console. Click on that to make the sub menu drop down and select ‘Add/Edit Taxonomies’:

How to Create WordPress Taxonomies Using a Plugin - Add or Edit Taxonomies

3. Add the details of your new taxonomy

You will now need to type the details of your new taxonomy into the corresponding fields, namely:

  • Taxonomy Slug – here, you should enter appropriate wording which will go into the URL. It is best to keep slugs as short as possible and only use alphanumeric, Latin characters. Also, use underscores instead of spaces.
  • Plural Label – enter the plural name of your taxonomy (e.g., Towns)
  • Singular Label – enter the singular name for your taxonomy (e.g., Town)
  • Auto-populate labels – clicking this will automatically fill in various other fields further down the screen. We suggest doing so as it can save time.
  • Attach to Post Type – select which post type(s) that will be using the taxonomy.

For example, your screen will look something like this:

How to Create WordPress Taxonomies Using a Plugin - Add details

4.  Add labels to your new taxonomy

Scroll down the page, and you will see a section titled ‘Additional Labels.’ If you hit the ‘Auto-populate Labels’ link as suggested in step 5 above, most of these fields will have been filled in automatically. You are free to edit these or add your own, and below each field is a description of how WordPress uses the labels:

How to Create WordPress Taxonomies Using a Plugin - Add Labels

Note that the labels are only used within the WordPress admin panel and will not show on your site.

5. Configure the settings for the taxonomy

Once you have finished inputting the labels, scroll further down the page to the ‘Settings’ area. Here you have many options to configure how your new taxonomy will behave. Again, there is a brief description of what each option does beneath each field.

Unless you are an advanced WordPress user, most of the settings can be left as they are. However, the most important is “Hierarchical.” That determines if the new taxonomy can be subdivided or not. In our example, if ‘towns’ were made hierarchical, we could have subdivisions such as ‘neighborhood,’ ‘district,’ and the like. On the other hand, if you leave this option as ‘false,’ then the taxonomy cannot have any parent-child relationships and will be multi-faceted.

How to Create WordPress Taxonomies Using a Plugin - Configure

Once you have configured the settings you require, you can now scroll to the bottom of the page and hit ‘Add Taxonomy.’

6. Use your new taxonomy

Whenever you now create or edit a post of the type(s) you selected in step 3, your new taxonomy will appear in the righthand menu of the post screen:

As a bonus, the Custom Post Type UI plugin also allows you to create custom post types in a similar fashion, and you can view the full details of all your custom taxonomies and post types by clicking on ‘Registered Type/Taxes’ under ‘CPT UI’ in the menu to the left of your WordPress admin console.

It’s a Wrap!

Adding custom taxonomies is a very powerful way of organizing the structure of your WordPress site. No longer are you limited to just categories and tags: you can now create the taxonomies you really do need. This is especially important for websites that are not blogs, but it can also be helpful on those.

If you have doubts about doing anything with codes and messing with .php files, we strongly recommend using a plugin to register taxonomies. Even if you are pretty savvy with coding, a plugin is still a good option as it can create your taxonomies quickly and easily, and you won’t need to be creating backups each time.

Have you created custom taxonomies for your WordPress site yet? Or do you run a basic blog where tags and categories adequately serve your needs? Please let us know in the comments section below, along with any taxonomy-related tips you may have.

Stay informed on WordPress

Every Friday you’ll receive news, tutorials, reviews, and great deals from the WordPress space.

Invalid email address
A team of WordPress experts that love to test out new WordPress related software, WordPress plugins and WordPress themes.