How To Create A Custom WordPress Taxonomy

WordPress taxonomies allow you to group your content together into similar sets of content. There are two types of default taxonomies built into WordPress these are categories and tags.

The difference between a category and a tag is that categories can be grouped in a hierarchy so you can have parent categories.

Tags give you more freedom then categories you can add new tags easily by typing them in the tags textbox on the post screen. Normally a post will have multiple tags but will only one/two categories.

Custom Taxonomies

Since WordPress 2.3 developers have been able to create their own custom taxonomies by using the function register_taxonomy(). This function will take 3 parameters, the name of the taxonomy, the post type to assign the taxonomy and arguments to define the taxonomy.

register_taxonomy( $taxonomy, $object_type, $args );

In this tutorial we're going to investigate how to create your own WordPress taxonomies you can use on your website.

add_action( 'init', 'create_movie_taxonomy' );

function create_movie_taxonomy() {
	register_taxonomy(
		'movie-genre',
		'post',
		array(
			'label' => __( 'Movie Genre' ),
			'rewrite' => array( 'slug' => 'movie-genre' ),
			'hierarchical' => true,
		)
	);
}

The above code will create a new taxonomy for the movie genre that you can assign to posts to group them correctly.

This function takes 3 parameters the first being the id of the taxonomy, next being the object type that the taxonomy is used on, the third is an array of arguments you can use to customise how the taxonomy works.

The arguments include:

  • Labels
    • name - General name for the taxonomy
    • singular_name - The singular name to the taxonomy
    • menu_name - Menu name
    • all_items - Name used to display all items
    • parent_item - Label used for define the parent item
    • parent_item_colon - Label used for parent item :
    • new_item_name - Label for new item
    • add_new_item - Label to add a new item
    • edit_item - Label to edit the item
    • update_item - Update the existing item
    • view_item - View the taxonomy label
    • separate_items_with_commas - How to separate the items
    • choose_from_most_used - Label used for most used
    • popular_items - Label to show popular items
    • search_items - Text used on search term
    • not_found - Text for items not found
  • public - This will decide if the taxonomy is publicly queryable
  • show_ui - To show the taxonomy in the admin area interface
  • show_in_menu - If to show the taxonomy on the admin menu
  • show_in_nav_menus - If you want to add the taxonomy to menus
  • show_tagcloud - If you want to allow the tagclud widget to use this taxonomy
  • show_in_quick_edit - If you want a quick edit link to the taxonomy
  • meta_box_cb - Callback function for any meta box
  • show_admin_column - If to add taxonomy column to post types
  • description - The description for the taxonomy
  • hierarchical - If the post is hierarchical like categories or not like tags
  • update_count_callback - Function for when WordPress counts for object type is updated
  • query_var - Change the query variable used for this taxonomy
  • rewrite - Defines how you want to generate the URL for the taxonomy
  • capabilities - Defines the capabilities for the taxonomy
  • sort - Defines if you want to sort the taxonomy
// Create new taxonomy
function create_new_taxonomy( )
{
    $labels = array(
        'name'                       => _x( 'taxonomies', 'Taxonomy General Name', 'text_domain' ),
        'singular_name'              => _x( 'taxonomy', 'Taxonomy Singular Name', 'text_domain' ),
        'menu_name'                  => __( 'Taxonomy', 'text_domain' ),
        'all_items'                  => __( 'All items', 'text_domain' ),
        'parent_item'                => __( 'Parent Item', 'text_domain' ),
        'parent_item_colon'          => __( 'Parent Item Colon:', 'text_domain' ),
        'new_item_name'              => __( 'New Item Name', 'text_domain' ),
        'add_new_item'               => __( 'Add New Item', 'text_domain' ),
        'edit_item'                  => __( 'Edit Item', 'text_domain' ),
        'update_item'                => __( 'Update Item', 'text_domain' ),
        'view_item'                  => __( 'View Item', 'text_domain' ),
        'separate_items_with_commas' => __( 'Separate Items With Commas', 'text_domain' ),
        'choose_from_most_used'      => __( 'Choose From Most Used', 'text_domain' ),
        'popular_items'              => __( 'Popular Items', 'text_domain' ),
        'search_items'               => __( 'Search Items', 'text_domain' ),
        'not_found'                  => __( 'Not Found', 'text_domain' ),
    );

    $args = array(
        'labels'                     => $labels,
        'hierarchical'               => true,
        'public'                     => true,
        'show_ui'                    => true,
        'show_in_menu'               => true,
        'show_admin_column'          => true,
        'show_in_nav_menus'          => true,
        'show_tagcloud'              => true,
        'show_in_quick_edit'         => true,
    );

    register_taxonomy( 'taxonomy', array( 'post', 'pages' ), $args );
}
add_action( 'init', 'create_new_taxonomy');

Coveloping has a WordPress Taxonomy Generator that gives you all the options you need to create a new WordPress taxonomy to use in your website, all you have to do is copy the generated code into a new plugin or a functions.php and you will see the taxonomy in your admin area.

WordPress Taxonomy Generator

Coveloping Membership

Access to a range of developer tools, get your first month free

Start Your Free Trial

Don't miss out!

Sign up to our newsletter for the latest news from Coveloping

Leave a Reply

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

Code Copied!