create custom taxonomy
Home » BLOG » WordPress » How to create a custom taxonomy in WordPress

How to create a custom taxonomy in WordPress

category:  WordPress

The previous post, I talk about how to create the custom post type. This post, I gonna talk about how to create a custom taxonomy in WordPress.

Let’s say. I want to create the new Keyword taxonomy for my project post type. I need to create the keyword for each project. The keyword works the same way as tags for the posts.

We will create our own plugin for creating the custom taxonomy.

Note that, I recommend you create the custom taxonomy in your own plugin rather than in your theme(functions.php) . This way, when you change your current theme, the content stored in custom taxonomies won’t disappear.

Create our own plugin

Let’s say I already created the project post type name as test_project_cpt. I want to create the custom taxonomy names Keyword for test_project_cpt post type only. Let’s do it.

  • Create a new custom-taxonomy folder under WordPress plugins folder(wp-content/plugins).
  • Create a new custom-taxonomy.php which is a main file for our plugin. We create this PHP file under the custom-taxonomy folder.
  • In the custom-taxonomy.php, we add the code below.
<?php

/**
 * Plugin Name: Custom Taxonomy
 * Description: detail
 * Version: 1.0 
 * Author: Apple Rinquest 
 * Author URI: https://applerinquest.com 
 */

if (!defined('ABSPATH')) {
    exit; // Exit if accessed directly.
}

// include our class files
require plugin_dir_path(__FILE__) . 'classes/class-register-keyword-terms.php';

Create custom taxonomy (just like tags for the posts)

  • In the code above, we include the class-register-keyword-terms file which we don’t create yet. First, we create the classes folder under our own plugin. Then we create our class file and add the code below.
<?php
// prevent to access the file directly
defined('ABSPATH') or die('No script kiddies please!');

// check the exists class to avoid the conflict with other plugins and theme
if (!class_exists('Keyword_Project_CPT')) {

    class Keyword_Project_CPT
    {
        /**
         * start up
         */
        public function __construct()
        {
            // add new keyword taxonomy for the project post type
            add_action('init', array($this, 'custom_keyword'));
        }

        /**
         * register new taxonomy 
         */
        public function custom_keyword()
        {
            $labels = array(
                'name' => 'Keywords',
                'singular_name' => 'Keyword',
                'search_items' =>  __('Search Keywords'),
                'all_items' => __('All Keywords'),
                'parent_item' => __('Parent Keyword'),
                'parent_item_colon' => __('Parent Keyword:'),
                'edit_item' => __('Edit Keyword'),
                'update_item' => __('Update Keyword'),
                'add_new_item' => __('Add New Keyword'),
                'new_item_name' => __('New Keyword'),
                'menu_name' => __('Keywords'),
                'not_found' => __('No Keywords found')
            );

            register_taxonomy(
                'test_keyword',  // The name of the taxonomy. Name should be in slug form (must not contain capital letters or spaces).
                'test_project_cpt',  // post type name
                array(
                    'hierarchical' => false,
                    'labels' => $labels,
                    'show_ui' => true,
                    'show_admin_column' => true,
                    'query_var' => true,
                    'rewrite' => array('slug' => 'test-keyword'),
                )
            );
        }
    }
}

// initiate class
$mms_project_ctp = new Keyword_Project_CPT();

From the code above, you already created the custom taxonomy names Keyword. After you enable our plugin, the new Keywords menu will show as below.

custom taxonomy
Figure: New Keywords Menu

Now, on the adding and editing form, you will see the Keyword taxonomy on the meta box.

custom term form
Figure: New Keywords Taxonomy

When you add the keywords via the form then save the form. Click on the Keywords menu. You will see the keywords below.

keyword custom terms
Figure: New Keywords Form

Create custom taxonomy (just like categories for the posts)

What is difference between creating the tags alike and categories alike with register_taxonomy function, is the hierarchical option. If the hierarchical is false, the custom taxonomy will work the same way as tags for the posts. If the hierarchical is true, the custom taxonomy will work the same way as categories for the posts.

Now, we add new custom_location function in the __construct function in our class file. The code is below.

// add new location for the project post type
add_action('init', array($this, 'custom_location'));

Then we add the new custom_location function with the code below in our class file.

        /**
         * register new taxonomy 
         */
        public function custom_location()
        {
            $labels = array(
                'name' => 'Locations',
                'singular_name' => 'Location',
                'search_items' =>  __('Search Locations'),
                'all_items' => __('All Locations'),
                'parent_item' => __('Parent Location'),
                'parent_item_colon' => __('Parent Location:'),
                'edit_item' => __('Edit Location'),
                'update_item' => __('Update Location'),
                'add_new_item' => __('Add New Location'),
                'new_item_name' => __('New Location'),
                'menu_name' => __('Locations'),
                'not_found' => __('No Locations found')
            );

            register_taxonomy(
                'test_location',  // The name of the taxonomy. Name should be in slug form (must not contain capital letters or spaces).
                'test_project_cpt',  // post type name
                array(
                    'hierarchical' => true,
                    'labels' => $labels,
                    'show_ui' => true,
                    'show_admin_column' => true,
                    'query_var' => true,
                    'rewrite' => array('slug' => 'test-location'),
                )
            );
        }

You will see the new Location menu.

custom taxonomy
Figure: New Locations Menu

When click on the “Add New Project” menu, you will see the form below. Notice that, the new Locations field shows in the meta box.

Figure: New Locations on the adding form

Here is the full code for our class file after adding custom taxonomies(Keywords and Locations).

<?php
defined('ABSPATH') or die('No script kiddies please!');

if (!class_exists('Keyword_Project_CPT')) {

    class Keyword_Project_CPT
    {
        /**
         * start up
         */
        public function __construct()
        {
            // add new keyword taxonomy for the project post type
            add_action('init', array($this, 'custom_keyword'));

            // add new location taxonomy for the project post type
            add_action('init', array($this, 'custom_location'));
        }

        /**
         * register new taxonomy 
         */
        public function custom_keyword()
        {
            $labels = array(
                'name' => 'Keywords',
                'singular_name' => 'Keyword',
                'search_items' =>  __('Search Keywords'),
                'all_items' => __('All Keywords'),
                'parent_item' => __('Parent Keyword'),
                'parent_item_colon' => __('Parent Keyword:'),
                'edit_item' => __('Edit Keyword'),
                'update_item' => __('Update Keyword'),
                'add_new_item' => __('Add New Keyword'),
                'new_item_name' => __('New Keyword'),
                'menu_name' => __('Keywords'),
                'not_found' => __('No Keywords found')
            );

            register_taxonomy(
                'test_keyword',  // The name of the taxonomy. Name should be in slug form (must not contain capital letters or spaces).
                'test_project_cpt',  // post type name
                array(
                    'hierarchical' => false,
                    'labels' => $labels,
                    'show_ui' => true,
                    'show_admin_column' => true,
                    'query_var' => true,
                    'rewrite' => array('slug' => 'test-keyword'),
                )
            );
        }

        /**
         * register new taxonomy 
         */
        public function custom_location()
        {
            $labels = array(
                'name' => 'Locations',
                'singular_name' => 'Location',
                'search_items' =>  __('Search Locations'),
                'all_items' => __('All Locations'),
                'parent_item' => __('Parent Location'),
                'parent_item_colon' => __('Parent Location:'),
                'edit_item' => __('Edit Location'),
                'update_item' => __('Update Location'),
                'add_new_item' => __('Add New Location'),
                'new_item_name' => __('New Location'),
                'menu_name' => __('Locations'),
                'not_found' => __('No Locations found')
            );

            register_taxonomy(
                'test_location',  // The name of the taxonomy. Name should be in slug form (must not contain capital letters or spaces).
                'test_project_cpt',  // post type name
                array(
                    'hierarchical' => true,
                    'labels' => $labels,
                    'show_ui' => true,
                    'show_admin_column' => true,
                    'query_var' => true,
                    'rewrite' => array('slug' => 'test-location'),
                )
            );
        }
    }
}

// initiate class
$mms_project_ctp = new Keyword_Project_CPT();

And that’s how you create the custom taxonomy for WordPress.

Create custom taxonomy with the marketing plugins

However, you can use the help from the plugins such as Custom Post Type UI plugin. If you don’t feel comfortable with coding by your self.

  •  
  •  
  •  
  •  
  •