Picture Text of How to Create Custom Taxonomies for Custom Post Types in WordPress

How to Create Custom Taxonomies for Custom Post Types in WordPress?

By default, WordPress allows you to sort out your content with categories and tags. However, with custom taxonomies you can further customize the way you sort your content. In this article, we’ll show you how to easily create custom taxonomies in WordPress. Lets start by defining custom taxonomy.

What is a WordPress Taxonomy?

A WordPress taxonomy is approach to organize groups of posts and custom post types. The word taxonomy comes from the biological classification method called Linnaean taxonomy. By default, WordPress comes with two taxonomies called categories and tags. You can use them to organize your blog posts. However, if you are using a custom post type, then categories and tags may not look suitable for all content types.

Adding Taxonomies For Custom Posts

Let’s use our custom post type “Services” and register taxonomy “Types” and “Features” like categories and tags respectively.

Creating a Hierarchical Taxonomy

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

//create a custom taxonomy name it subjects for your posts
function webtut_custom_hierarchical_taxonomy() {

    // Add new taxonomy, make it hierarchical like categories

    $singular = 'Type';
    $plural = 'Types';

    $labels = array(
        'name' => _x( $plural, 'webtut' ),
        'singular_name' => _x( $singular, 'webtut' ),
        'search_items' =>  __( 'Search ' . $plural ),
        'all_items' => __( 'All ' . $plural ),
        'parent_item' => __( 'Parent ' . $singular ),
        'parent_item_colon' => __( 'Parent ' . $singular . ':' ),
        'edit_item' => __( 'Edit ' . $singular ), 
        'update_item' => __( 'Update ' . $singular ),
        'add_new_item' => __( 'Add New ' . $singular ),
        'new_item_name' => __( 'New ' . $singular . ' Name' ),
        'menu_name' => __( $plural ),
    );

    // Now register the taxonomy
    register_taxonomy('services', array('types'), array(
        'hierarchical' => true,
        'labels' => $labels,
        'show_ui' => true,
        'show_in_rest' => true,
        'show_admin_column' => true,
        'query_var' => true,
        'rewrite' => array( 'slug' => 'type' ),
    ));

}

Creating a Non-hierarchical Taxonomy

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

//create a custom taxonomy name it subjects for your posts
function webtut_custom_nonhierarchical_taxonomy() {

    // Add new taxonomy, make it hierarchical like categories

    $singular = 'Feature';
    $plural = 'Features';

    $labels = array(
        'name' => _x( $plural, 'webtut' ),
        'singular_name' => _x( $singular, 'webtut' ),
        'search_items' =>  __( 'Search ' . $plural ),
        'all_items' => __( 'All ' . $plural ),
        'parent_item' => __( 'Parent ' . $singular ),
        'parent_item_colon' => __( 'Parent ' . $singular . ':' ),
        'edit_item' => __( 'Edit ' . $singular ), 
        'update_item' => __( 'Update ' . $singular ),
        'add_new_item' => __( 'Add New ' . $singular ),
        'new_item_name' => __( 'New ' . $singular . ' Name' ),
        'menu_name' => __( $plural ),
    );

    // Now register the taxonomy
    register_taxonomy('services', array('features'), array(
        'hierarchical' => false,
        'labels' => $labels,
        'show_ui' => true,
        'show_in_rest' => true,
        'show_admin_column' => true,
        'query_var' => true,
        'rewrite' => array( 'slug' => 'feature' ),
    ));

}

Remember to replace the taxonomy name and labels with your own taxonomy labels. You will see that this taxonomy is associated with the Services post type, you’ll need to change that to whatever post type you want to use it with.

Notice the difference between the 2 codes. Value for hierarchical argument is true for category-like taxonomy and false for tags-like taxonomies.

Displaying Custom Taxonomies

Since we have created custom taxonomies your WordPress theme will still not display them. In order to show them, you’ll need to add some code to your WordPress theme or child theme. This code will need to be added in templates files where you want to display the terms. Usually, it is single.php, content.php, or one of the files inside the template-parts folder in your WordPress theme.

You will need to add the following code where you want to display the terms.

<?php the_terms( $post->ID, 'types', 'Types: ', ', ', ' ' ); ?>

You can add it in other files as well such as archive.php, index.php, and anywhere else you want to display the taxonomy.

Leave a Reply

%d bloggers like this: