Today’s tutorial we are showing how to create a custom taxonomy in WordPress. After WordPress 3 this feature is included in the core, so we just need to use the function with some params. The core developers created register_taxonomy()
function to handle the heavy lifting for us.
Why Custom Taxonomies?
As your system/website grows you feel the default categories in WordPress is not enough to classify the articles in the website. So it would be beneficial if you create your own classification. But how do you create your own taxonomy in WordPress ?. The good news is WordPress allows developers to create Custom Taxonomies easily.
For example if you are making a gift catalogue website. You will have many products in different categories and gifts aimed for different groups of people such as gifts for him, gifts for her. If you have already used the categories taxonomy, so there should be a new one. It’s very easy to create a custom taxonomy in WordPress, just use the code.
You can use this code in functions.php in themes or in your module file.
function wporg_register_taxonomy_gifts() { $labels = [ 'name' => _x('Gifts', 'taxonomy general name'), 'singular_name' => _x('Gift', 'taxonomy singular name'), 'search_items' => __('Search Gifts'), 'all_items' => __('All Gifts'), 'parent_item' => __('Parent Gift'), 'parent_item_colon' => __('Parent Gift:'), 'edit_item' => __('Edit Gift'), 'update_item' => __('Update Gift'), 'add_new_item' => __('Add New Gift'), 'new_item_name' => __('New Gift Name'), 'menu_name' => __('Gifts'), ]; $args = [ 'hierarchical' => true, // make it hierarchical (like categories) 'labels' => $labels, 'show_ui' => true, 'show_admin_column' => true, 'query_var' => true, 'show_in_quick_edit'=> true, 'show_in_rest' => true, 'rewrite' => ['slug' => 'gift'], ]; register_taxonomy('gift', ['post'], $args); } add_action('init', 'wporg_register_taxonomy_gifts');
Important Information
If you are using the Gutenberg editor you should set the ‘show_in_rest’ => true, otherwise new custom taxonomy will not appear in the post add/edit screen.
- Just want to thank us? Buy us a Coffee
- May be another day? Shop on Amazon using our links.
Your prices won't change but we get a small commission.
Leave a Reply