Taxonomies are used for categorizing posts in WordPress. Each WordPress installation comes with a default taxonomy commonly known as Category but how about you want to create your own category and give it your own name?

How to register a custom taxonomy in Wordpress

Truth is registering Taxonomies is something you can’t run away from considering you are deep into WordPress development. The code below explains how to register a simple Taxonomy in WordPress. Simply paste it in the theme’s functions.php and you are good to go.

function myposts_taxonomy() {  
    register_taxonomy(  
        'my_categories',  //The name of the taxonomy. Name should be in slug form (must not contain capital letters or spaces). 
        'mypost',        //post type name
        array(  
            'hierarchical' => true,  
            'label' => 'My Posts',  //Display name
            'query_var' => true,
            'rewrite' => array(
                'slug' => 'myposts', // This controls the base slug that will display before each term
                'with_front' => false // Don't display the category base before 
            )
        )  
    );  
}  
add_action( 'init', 'myposts_taxonomy');

To change the URL of the Taxonomy, simply paste the following code in your theme’s functions.php file (Replace my_categories with your custom taxonomy name).

function filter_post_type_link($link, $post)
{
    if ($post->post_type != 'themes')
        return $link;

    if ($cats = get_the_terms($post->ID, 'themes_categories'))
        $link = str_replace('%my_categories%', array_pop($cats)->slug, $link);
    return $link;
}
add_filter('post_type_link', 'filter_post_type_link', 10, 2);

In the code above, my_categories is the name of the taxonomy you are registering. ‘mypost’ is the name of the post that is accessing the taxonomy i.e incase a custom post type is registered then it’s name should replace ‘mypost’.

Now i guess you have looked at WordPress’ default Category layout and seen the Uncategorized Category. Uncategorized can’t be deleted simply because it’s the fallback category for posts that are nor assigned to Categories (Taxonomies). If this category didn’t exist then all posts that assinged with no category would return a 404 error page. Same applies here, if you are creating a custom Taxonomy then you shouldn’t forget to create a fallback category for posts that are not assigned to it. You can achieve that by pasting the code below in your theme’s functions.php.

function default_taxonomy_term( $post_id, $post ) {
    if ( 'publish' === $post->post_status ) {
        $defaults = array(
            'my_categories' => array( 'other'),   //

            );
        $taxonomies = get_object_taxonomies( $post->post_type );
        foreach ( (array) $taxonomies as $taxonomy ) {
            $terms = wp_get_post_terms( $post_id, $taxonomy );
            if ( empty( $terms ) && array_key_exists( $taxonomy, $defaults ) ) {
                wp_set_object_terms( $post_id, $defaults[$taxonomy], $taxonomy );
            }
        }
    }
}
add_action( 'save_post', 'default_taxonomy_term', 100, 2 );

Don’t forget to change ‘my_categories’ to your custom taxonomy name otherwise, the code should do the trick for you.