How to create custom taxonomies in wordpress without plugin?

In this article we will teach you how to create custom taxonomies in wordpress without plugin.

taxonomy in WordPress is a way of grouping posts, articles or custom posts together. WordPress has some default taxonomies categories and tags. Sometimes we needs the custom taxonomies for our custom posts types likes songs categories, movies categories, projects categories, news categories etc.

So, Your Taxonomies can be hierarchical like categories and non-hierarchical like tags. We can create custom taxonomies by using two methods given below:

Custom Taxonomies in WordPress with Plugin

If you want to create custom taxonomies in wordpress with plugin then you can use the Custom Post Type UI plugin. Custom Post Type UI provides an easy to use interface for registering and managing custom post types and taxonomies for your Website or Blog.

Custom Taxonomies in WordPress Without Plugin

If you want to create custom taxonomies in wordpress without plugi. Then paste the code given below in your functions.php file located at website_root_path/wp-content/themes/your_theme/functions.php. Here below in our code we are going to create our custom taxonomy song_categories.

//create a custom taxonomy name it song categories for your posts
 
function create_song_categories_hierarchical_taxonomy() {
 
// Add new taxonomy, make it hierarchical like categories
//first do the translations part for GUI
 
  $labels = array(
    'name' => _x( 'Song Categories', 'taxonomy general name' ),
    'singular_name' => _x( 'Song Category', 'taxonomy singular name' ),
    'search_items' =>  __( 'Search Song Categories' ),
    'all_items' => __( 'All Song Categories' ),
    'parent_item' => __( 'Parent Song Category' ),
    'parent_item_colon' => __( 'Parent Song Category:' ),
    'edit_item' => __( 'Edit Song Category' ), 
    'update_item' => __( 'Update Song Category' ),
    'add_new_item' => __( 'Add New Song Category' ),
    'new_item_name' => __( 'New Song Category' ),
    'menu_name' => __( 'Song Categories' ),
  );    
 
// Now register the taxonomy
  register_taxonomy('song_categories',array('songs'), array(
    'hierarchical' => true,
    'labels' => $labels,
    'show_ui' => true,
    'show_in_rest' => true,
    'show_admin_column' => true,
    'query_var' => true,
    'rewrite' => array( 'slug' => 'song_categories' ),
  ));
 
}

//hook into the init action and call create_book_taxonomies when it fires
 
add_action( 'init', 'create_song_categories_hierarchical_taxonomy', 0 );

In the code above we have used the function register_taxonomy and you can read here in detail. We named our taxonomy song_categories. We have used the custom post type songs. You can read our article how to create custom post type in wordpress without plugin. You can also use the default post types like post, page, attachment, revision etc.

If you want to create custom taxonomy like tags then in the code above set hierarchical option to false.

If you want more tutorials and tricks about wordpress then visit our WordPress Page and follow us on facebooktwittertumblrlinkdedin and if you like this article then share this.