Skip to content

How To Create Custom Taxonomy In WordPress

WordPress allows you to organize your posts in categories and tags. By default WordPress has three built-in taxonomies:

  • Category: It allows you to group your posts together by sorting them into various categories.
  • Tag: Tags allows you to group your posts together in a freeform. Tags can be made by simply typing them.
  • Link Category: Link category let you categorize your links. Nothing special about them.
  • Navigation Menu: The taxonomy we use to manage your navigation menus.

WordPress’ custom taxonomy feature allows you to create your own custom category or tag style taxonomies to manage your posts and extend your WordPress. You can create your own Wikipedia or IMDb.

For an example, IMDb categorizes movies on their website within various taxonomies, such as Directors, Writers, Actors & more.

Create Custom Taxonomy

Now we will show you to easily create custom taxonomy and assign them to your posts or custom post types.

[alert style=”green”]Easy Way Out: You can also simply generate Custom Taxonomy with generatewp.[/alert]

First we will start our custom taxonomy plugin by adding following header to the plugin file:

<?php
/*
Plugin Name: Teams
Plugin URI: https://www.trickspanda.com/
Description: The code we use to power Team taxonomy in our website.
Version: 1.0
Author: Hardeep Asrani
Author URI: http://www.hardeepasrani.com
License: GPLv2
*/
 
?>

We will create a custom taxonomy for teams. Before the closing PHP command, add the following action to our plugin:

// Hook into the 'init' action
add_action( 'init', 'team_taxonomy', 0 );

Now, we will register and add elements to our taxonomy with following code:

// Register Custom Taxonomy
function team_taxonomy() {

    $labels = array(
        'name'                       => _x( 'Teams', 'Taxonomy General Name', 'team_taxonomy' ),
        'singular_name'              => _x( 'Team', 'Taxonomy Singular Name', 'team_taxonomy' ),
        'menu_name'                  => __( 'Teams', 'team_taxonomy' ),
        'all_items'                  => __( 'All Teams', 'team_taxonomy' ),
        'parent_item'                => __( 'Parent Team', 'team_taxonomy' ),
        'parent_item_colon'          => __( 'Parent Team:', 'team_taxonomy' ),
        'new_item_name'              => __( 'New Team', 'team_taxonomy' ),
        'add_new_item'               => __( 'Add New Team', 'team_taxonomy' ),
        'edit_item'                  => __( 'Edit Team', 'team_taxonomy' ),
        'update_item'                => __( 'Update Team', 'team_taxonomy' ),
        'separate_items_with_commas' => __( 'Separate Teams with commas', 'team_taxonomy' ),
        'search_items'               => __( 'Search Teams', 'team_taxonomy' ),
        'add_or_remove_items'        => __( 'Add or remove Teams', 'team_taxonomy' ),
        'choose_from_most_used'      => __( 'Choose from the most used teams', 'team_taxonomy' ),
        'not_found'                  => __( 'Not Found', 'team_taxonomy' ),
    );
    $args = array(
        'labels'                     => $labels,
        'hierarchical'               => true,
        'public'                     => true,
        'show_ui'                    => true,
        'show_admin_column'          => true,
        'show_in_nav_menus'          => true,
        'show_tagcloud'              => true,
    );
    register_taxonomy( 'team', array( 'post' ), $args );

}

In the first part of our code, we have entered the taxonomy name and all other basic stuff. Nothing special to learn here. Just replace Team word with your taxonomy name.

Second part of the code contains all the configuration about our taxonomy.

  • ‘hierarchical’ => true determines the structure of our taxonomy. Categories have the hierarchical structure, while the tags have a non-hierarchical structure.
  • ‘public’ => true determines the visibility of our taxonomy both in front and back-end.
  • ‘show_ui’ => true determines whether to show taxonomy in the admin menu.
  • ‘show_admin_column’ => true determines whether to show taxonomy columns on associated post-types.
  • ‘show_in_nav_menus’ => true determines whether to make our taxonomy available for selection in Navigation Menus.
  • ‘show_tagcloud’ => true determines whether to show in tag cloud widget.

The last line of your code assigns our taxonomy to post types.

Save your plugin file. Now, just upload and activate this plugin to get started with your new custom taxonomy. You can create or edit your posts to play with your new taxonomy. Don’t forget to leave a comment if you have any questions.

2 thoughts on “How To Create Custom Taxonomy In WordPress”

Leave a Reply to Harshal Limaye Cancel reply

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