How To Create Custom Taxonomies In WordPress: A Step-by-Step Guide

How to Create custom Taxonomies in WordPress

WordPress, by default, provides categories and tags for structuring your content. However, you can create custom taxonomies in WordPress to organize your content based on your website requirements.

WordPress is a versatile and powerful content management system (CMS) that allows you to create and manage a wide range of websites, from simple blogs to complex sites.

As your website becomes more complicated, you’ll find that organizing your content becomes increasingly important. This is where custom post types and custom taxonomies become handy.

In this article, we will learn to create custom taxonomies in WordPress.

Understanding Taxonomies in WordPress

Before we jump into creating custom taxonomies, let’s understand the basics. In WordPress, taxonomies are a way to group and organize content, primarily for posts and custom post types.

There are two types of taxonomies, hierarchical and non-hierarchical. Hierarchical taxonomies allow you to create parent-child relationships between terms whereas non-hierarchical taxonomies don’t have any hierarchical structure.

The default taxonomies in WordPress are categories and tags. Categories are hierarchical, providing a structured way to organize content, while tags are non-hierarchical and offer flexibility in content grouping.

What are Custom Taxonomies in WordPress?

Custom taxonomies are user-defined taxonomies that enable you to create a more personalized and organized structure for your content. This feature is particularly useful when standard categories and tags don’t quite meet the specific needs of your website.

For instance, imagine you run a food blog, and alongside default categories like “Recipes” and “Tips,” you want a custom taxonomy called “Cuisine” to categorize recipes by the type of cuisine they belong to, such as Italian, Mexican, or Asian.

How to Create Custom Taxonomies in WordPress With a Plugin?

To create custom taxonomy in WordPress, you can use plugins. If you prefer this approach then you can install and activate plugins like Custom Post Type UI or Toolset Types. These plugins help you create custom taxonomies without writing any code. However, if you want more control and flexibility, you can create custom taxonomies in WordPress manually.

Steps To Create Custom Taxonomy in WordPress Manually

Now, let’s dive into the step-by-step process of creating a custom taxonomy in WordPress.

Step 1: Preparing Your WordPress Environment

If you don’t have a WordPress environment running yet, please view our article about how to install WordPress. If you have a WordPress website running, ensure you have a recent backup of your site, and access your WordPress admin dashboard.

Step 2: Registering a Custom Taxonomy

WordPress has a built-in function register_taxonomy to create a custom taxonomy. The function accepts 3 parameters. The first is the taxonomy key, the second parameter is for the post type with which the taxonomy should be associated, and the third one is the array or query string of arguments for registering a taxonomy.

In this article, we will create a custom taxonomy ‘genre‘ for the custom post type ‘music‘. You can find information about custom post types here. Add the following code in the functions.php file of the active theme from the theme directory. Please make sure you work on the child theme if you are using any prebuilt themes.

/*
*
* Creating a function to create our custom taxonomy
*/
function create_custom_taxonomy() {
	$labels = array(
		'name'              => __( 'Genres', 'Taxonomy General Name', 'your-text-domain' ),
		'singular_name'     => __( 'Genre', 'taxonomy singular name', 'your-text-domain' ),
		'search_items'      => __( 'Search Genres', 'your-text-domain' ),
		'all_items'         => __( 'All Genres', 'your-text-domain' ),
		'parent_item'       => __( 'Parent Genre', 'your-text-domain' ),
		'parent_item_colon' => __( 'Parent Genre:', 'your-text-domain' ),
		'edit_item'         => __( 'Edit Genre', 'your-text-domain' ),
		'update_item'       => __( 'Update Genre', 'your-text-domain' ),
		'add_new_item'      => __( 'Add New Genre', 'your-text-domain' ),
		'new_item_name'     => __( 'New Genre Name', 'your-text-domain' ),
		'menu_name'         => __( 'Genres', 'your-text-domain' ),
	);

	$args = array(
		'hierarchical'      => true,
		'labels'            => $labels,
		'show_ui'           => true,
		'show_admin_column' => true,
		'query_var'         => true,
		'rewrite'           => array( 'slug' => 'genre' ),
	);

	register_taxonomy( 'genre', 'music', $args );
}

add_action( 'init', 'create_custom_taxonomy', 0 );

In the above code, the variable $labels defines all the labels for the custom taxonomy. Also, genre behaves as a hierarchical taxonomy. If you want to make it non-hierarchical, you can simply set the ‘hierarchical’ argument to false.

Once you add the code, please go to Settings > Permalinks and click on the Save Changes button which will make the necessary .htaccess changes for the recently created taxonomy.

Step 3: Accessing Custom Taxonomies in WordPress

It is easy to access the recently created taxonomy from the WordPress dashboard by hovering over the post type to which you assigned the taxonomy.

Recently Created Custom Taxonomy in WordPress

You should be able to see the Genres taxonomy added to Music Post Type where you can add the respective terms according to your requirements.

Step 4: Adding Custom Taxonomies Terms in WordPress

Once you are on the taxonomy page in the WordPress backend, it is easier to add terms to the recently created taxonomy. These terms are what can be assigned to a post and can be grouped under these terms.

Create custom taxonomies in WordPress

Step 5: Assigning a Taxonomy Term To a Post

You can now create a post and assign a term from the right-hand side of the page. The interesting thing is that the page allows you to create new terms right from there.

Create a Post and Assign a Custom Taxonomy Terms

Also, you should be able to select the respective genre while creating a new music post.

Conclusion

In conclusion, creating custom taxonomies in WordPress offers a dynamic way to organize your content. And that’s it. This is how you can create custom taxonomies in WordPress.

We hope that this article has given you the information you need to create custom taxonomies in WordPress without using any plugins.

FAQs on How to Create Custom Taxonomies in WordPress

How do I create a custom taxonomy in WooCommerce?

WooCommerce uses its own “product” custom post type for product items. So, when registering your custom taxonomy (let’s call it “brand”), follow this article and while registering the taxonomy, simply set the post type parameter to “product” like this: register_taxonomy( 'brand', 'product', $args ). It’s that simple! Now, you have added custom taxonomy in WooCommerce.

What is the difference between taxonomy and category in WordPress?

In WordPress, a “taxonomy” is a larger organizational system that contains “categories” as one type of taxonomy. Categories are often used in a hierarchical form to organize and classify material such as blog entries, containing parent and child categories. In contrast, “taxonomy” refers to the complete system, which includes not just categories but also tags (non-hierarchical) and other custom taxonomies.

How do I create multiple taxonomies in WordPress?

You can create taxonomies in WordPress using the register_taxonomy function, and you can use it as many times as needed. Simply adjust the function’s parameters like the taxonomy key, post type object/array, and arguments variable ($args) to suit your specific needs. To learn how to create custom taxonomies in WordPress, refer to this article, and repeat the process as necessary to create multiple taxonomies for your website.

You might also like