Creating Your First Custom Taxonomy in WordPress

custom taxonomy

Tag and category are two default WordPress taxonomies. They help to manage and grouping your WordPress posts. However, sometimes that isn’t enough, especially when you have Custom Post Type. For example, if you have Portfolio CPT, having ‘Type’ or ‘Price Range’ taxonomy will help you grouping all the portfolio items differently from normal posts. For more information about Custom Taxonomy, WordPress Codex is one of the main resources you should refer to.

Create a Custom Taxonomy

In this example, I’m creating Type custom taxonomy and assign it to Portfolio CPT. You may copy and paste the snippet below to your functionality plugin. This custom taxonomy helps me to group or classify my portfolio based on the type of the project I’ve done.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
<?php
add_action( 'init', 'afn_register_taxonomy_types' );
function afn_register_taxonomy_types() {
$labels = array(
'name' => _x( 'Types', 'types' ),
'singular_name' => _x( 'Type', 'types' ),
'search_items' => _x( 'Search Types', 'types' ),
'popular_items' => _x( 'Popular Types', 'types' ),
'all_items' => _x( 'All Types', 'types' ),
'parent_item' => _x( 'Parent Type', 'types' ),
'parent_item_colon' => _x( 'Parent Type:', 'types' ),
'edit_item' => _x( 'Edit Type', 'types' ),
'update_item' => _x( 'Update Type', 'types' ),
'add_new_item' => _x( 'Add New Type', 'types' ),
'new_item_name' => _x( 'New Type', 'types' ),
'separate_items_with_commas' => _x( 'Separate types with commas', 'types' ),
'add_or_remove_items' => _x( 'Add or remove types', 'types' ),
'choose_from_most_used' => _x( 'Choose from the most used types', 'types' ),
'menu_name' => _x( 'Types', 'types' ),
);
$args = array(
'labels' => $labels,
'public' => true,
'show_in_nav_menus' => true,
'show_ui' => true,
'show_tagcloud' => true,
'show_admin_column' => false,
'hierarchical' => true,
'rewrite' => true,
'query_var' => true
);
register_taxonomy( 'types', array('Portfolio'), $args );
}
view raw functions.php hosted with ❤ by GitHub

The code might look complicated at first but it’s not. Here are some important parameters that you should know:

'name' - This is the name of you custom taxonomy. If you wish to create a custom taxonomy other than ‘Type’, simply change your taxonomy name here. Moreover, don’t forget to change the word ‘Type’ or ‘Types’ from line 8 to 22.

'show_in_nav_menus' - If you want to display your custom taxonomy, this should be set to true.

'hierarchical' - Setting this to true will make your custom taxonomy behaves like Category, while setting it to false will make your custom taxonomy behaves like Tag.

register_taxonomy( 'types', array('Portfolio'), $args ); - In line 38, this is where I assigned this custom taxonomy to my Portfolio CPT. If you want to assign it to multiple post types, you may do so by using a comman. Example: register_taxonomy( 'types', array('Post, Portfolio, Gallery'), $args );

It’s important to know that the code above does not include all parameters that can be accepted by the register_taxonomy functions. For complete list of all parameters, please refer Parameters - register_taxonomy page on WordPress Codex.

Making Life Easier with Custom Taxonomy Generator

If you build lots of sites for clients, you’ll eventually notice that you can simply reuse the code over and over again with only some minor modifications. That’s the reason why custom taxonomy generator is very useful and becomes very handy.

One of the great tools available is WordPress Custom Taxonomy Code Generator by Themergency.

Believe it or not, the code I’ve just presented above is generated by this tool. It rocks and is very handy.

We hope this tutorial helped you to create you first taxonomy and if you’ve any question, feel free to leave your question in the comment section below.

Leave a Reply

Your email address will not be published. Required fields are marked *