Registering Custom Taxonomy in WordPress

In one of my tutorial post, I showed how to register a Custom Post Type (CPT) in WordPress. Now, if you want to make it little more interesting and want to add “Categories” just like the regular blog posts, you can easily do that simply registering a taxonomy and assigning it to be used for your desired CPT.

Since on my example I already created a post type called “Books”, I would register a taxonomy so that I can create categories for “Books” post type. Definitely you can use it for your own CPT. On this example we would be using register_taxonomy function. Let’s take a look at the default function to register a custom taxonomy first.

<?php register_taxonomy($taxonomy,$object_type,$args); ?>

We would definitely be using this function but with little twist on it. Let’s take a look at the actual snippet. Here we go.

function register_custom_taxonomy(){
   register_taxonomy('bookscat', array('books'), array(
      'labels' => array(
         'name' => 'Categories',
         'singular_name' => 'Category',
         'search_items' => 'Search Category',
         'all_items' => 'All Categories',
         'parent_item' => 'Parent Category',
         'parent_item_colon' => 'Parent Category:',
         'edit_item' => 'Edit Category', 
         'update_item' => 'Update Category',
         'add_new_item' => 'Add New Category',
         'new_item_name' => 'New Category',
         'menu_name' => 'Categories'),
      'hierarchical' => true,
      'query_var' => true,
      'public' => true,
      'show_tagcloud' => true,
      'show_admin_column' => true,
      'show_in_nav_menus' => true,
      'sort' => '',
      'rewrite' => array(
         'slug' => 'books', 
         'hierarchical' => true,
         'with_front' => false),
      'show_ui' => true)); }

This is pretty simple and self explanatory. Now, there are quite a few important things that needs to be taken on your serious consideration. On my arguments, I set “hierarchical” parameter’s value to “true”. This would give you default posts category like “checkbox” widget on the right hand side of you post editing window. If you set the value to “false”, it would give you a widget similar to the “Tags” widget.

Important Notes

If you take a look at the default register taxonomy function carefully, you would see that it takes three different parameter, $taxonomy, $object_type and $args. First $taxonomy, it is the name of the taxonomy that should be registered. In my example I used “bookscat”. Here, you can write whatever word you want but always remember the reserved terms which should be avoided all the time.

Another parameter is $object_type or in other word object name. In this case that should be our desired custom post type (books), which would be using this taxonomy. This is very important as much as it is interesting. You can assign single post type or multiple post type with an array. Thus, this taxonomy would be available for all the post type your mention over here. In my example I used array('books') so that it can be used with “books” post type. As I mentioned earlier, you can also add other post types if you want. How cool is that?

Last parameter is $args which would be taking all other necessary arrays and their values.

Another, important issue I willingly skipped is the “capabilities” argument which I think a bit complex. In a simple sentence this is array of capabilities for this taxonomy which can be used with various roles or even custom user roles. I am definitely going to be writing on this issue as well in another post as it would require understanding of quite a few ideas first.

Today In History



Leave a Reply

Note: Convet HTML, PHP, JavaScripts from HTMLify, before posting from comment section.
License: By submitting a comment here you grant this site a perpetual license to reproduce your words and name/Web site in attribution. Please use your real name or a pseudonym (i.e., pen name, alias, nom de plume) when commenting. If you add your site name, company name, or something completely random, I'll likely change it to whatever I want.