WordPress Custom Taxonomy Register

WordPress Custom Taxonomy Register

Sometimes we need to use custom taxonomy with a custom post. WP have default taxonomy tags and category in the post. Now I will create a custom post with a custom taxonomy. Follow the below steps.

Step 1:
First, you have to register our custom post. I have registered the custom post type with the project name. Add this code in your theme functions.php or your theme plugin.

//Custom Post
function cpt_toolkit_custom_post()
{
    //Project Custom Post
    register_post_type('project',
        array(
            'labels' => array(
                'name' => esc_html__('Projects', 'text-domain'),
                'singular_name' => esc_html__('Project', 'text-domain'),
            ),
            'menu_icon' => 'dashicons-images-alt',
            'supports' => array('title', 'thumbnail', 'editor', 'excerpt'),
            'public' => true,
        )
    );
}
add_action('init', 'cpt_toolkit_custom_post');

Step2:

After creating we need to register custom taxonomy. For creating custom taxonomy we need custom post type name. I have already created a custom post type and my post type name is  ‘project’.

//Custom Taxonomy  
function cpt_custom_post_taxonomy(){
    register_taxonomy(
      'project_cat',
      'project',
        array(
          'hierarchical'      => true,
          'label'             => esc_html__('Projects Category', 'text-domain' ),
          'query_var'         => true,
          'show_admin_column' => true,
              'rewrite'         => array(
              'slug'          => 'project-category',
              'with_front'    => true
              )
        )
      );
  }
add_action('init', 'cpt_custom_post_taxonomy');

Now you can see the custom post and custom taxonomy in your dashboard.

Leave a Reply

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