Tutorials

How To Create Custom Taxonomies To Improve WordPress Site Usability

Pinterest LinkedIn Tumblr

One of the biggest reason that makes WordPress popular among users is the amazing customization capabilities it. From creating a custom post type to widgets, and from taxonomies to menus, WordPress enables you to fine tune your website according to your specific needs.

In this post, we will explore how to manage and create custom taxonomies in WordPress for increasing your website usability. For this purpose, we’ll be creating a custom taxonomy. But, before diving into any further details about custom taxonomy, let’s first have an overview of taxonomy.

A Brief Introduction to WordPress Taxonomy

Taxonomy in WordPress is a mechanism that helps to group posts in a hierarchy. Two of the most important default WordPress built-in taxonomies are “Categories” and “Tags”.

The “Category” taxonomy allows you to group the blog posts together in different categories. It is a hierarchical taxonomy that contains parent and child categories as shown in the image below:

wordpress-taxonomy

The “post_tag” taxonomy is just like the “category” taxonomy but is written in a non-hierarchical form. This means that it doesn’t contain any parent and child elements, and instead present information in a free form. Let’s say, for example, the category ‘Dessert’ contains tags like nuts, baking, etc.

wordpress-taxonomy

If you’ve worked on creating WordPress blog posts, then you’ll most likely be aware of how categories and tags are used to organize blog posts. But, what if you need to organize the custom post types in WordPress? You can use custom taxonomies to achieve such an objective.

Benefits of Custom Taxonomies

Custom taxonomies help to classify your website content in an organized way, making it easy for you and your site visitors to quickly search for the content that needs to be viewed. And thereby, increases the usability of your site.

How To Create a Custom Taxonomy?

With the introduction of WordPress version 2.3, users were provided with the ability to create their own choice of taxonomies. But this concept, however, started gaining popular when WP version 2.9 was rolled out. You can create and add a custom taxonomy in a WordPress website in different ways, as listed below:

  • Write code manually
  • Auto-generate code using Generate WP.
  • Use plugin such as Pods, Types or others for custom content types that enable users to create custom taxonomies.

The most recommended one is to use a plugin, as it helps you create custom taxonomies and add them in the WordPress backend without the need of writing any code. However, if you want to implement the other two techniques, then it would be better to create a custom plugin, as it will make the process of inserting code into functions.php relatively easier.

In order to create a custom plugin, simply copy the code of custom taxonomy as given below and paste it into a text file. But prior to that, make sure to add the following line of code at the top of the text file:

/* Plugin name: Custom Taxonomy */.

Let us now see the code snippet you’ll need to use for creating a custom plugin that supports custom taxonomy: User.

<?php

/* Plugin Name: User Taxonomy */

if ( ! function_exists( 'user_init' ) ) {
// Function used to register custom taxonomy
function user_init() {
// create a new taxonomy
register_taxonomy(

'user','post',

array('label' => __( 'User' ),
'rewrite' => array( 'slug' => 'user1' ),
'capabilities' => array(
'assign_terms' => 'edit_guides',

'edit_terms' => 'publish_guides')));
}

// Hook into the 'init' action

add_action( 'init', 'user_init' );
}

?>

As you can see in the code above, we’ve defined a taxonomy with the name “user”. It can be used to define the work to users for posts. In addition, a rewrite slug has been defined to change the URL into ‘/user1/’ rather than ‘/user/’. Apart from this, there is a capability line which is optional. If you don’t define the capabilities, then WordPress will assign users with default capabilities that are defined for the blog posts. For instance, the custom “edit_guides” content type capability will assign the taxonomy to the post. In addition, the user having the custom “publish_guides” capability will be able to create new taxonomy items.

Note: Make sure to add the above code in your theme’s “functions.php” file.

How to Use Custom Taxonomy?

So, now as you added a custom taxonomy, you’ll find a new meta box added on WordPress posts for you. The new meta box will appear just in your website admin sidebar underneath the “Posts” menu. The meta box looks just like “tags” box, allowing you to add tags to the posts. However, you’ll have to add some terms to your taxonomy to make the best possible use of it. For doing so, you will need to use wp_set_object_terms() function. Let’s consider an example to see how some terms can be added in a post in your taxonomy:

wp_set_object_terms( 346, 'xyz', 'user1' );

In this code, the term “xyz” is getting added to the post with an ID number 346 in the “user1” taxonomy.

Conclusion

Here’s hoping that after going through this post, you will better understand how you can organize your WordPress website content using custom taxonomies. Irrespective of whether you’re using a hierarchal or non-hierarchal taxonomy, implementing it in the right way will help make the content displayed on the website in a highly organized manner. The best part is that WordPress provides required tools, using which you can create custom taxonomies. However, which one you should choose depends on you.

Noor Mustafa Raza

I am a WordPress Developer and Designer, author @WPArena. I am providing Free WordPress consultation and can help you to install WordPress in a secure way to small businesses and bloggers.

Write A Comment

Pin It