Custom Taxonomy Hierarchy for Custom Post Types (eg Categories and Subcategories)

I’m working on building some custom taxonomies for a custom post type I created.

The custom post type is Products. For the custom post type Products I’ve created a taxonomy of Category. Now Under Category, I’d like to have a taxonomy of Sub_Category. I’ve created all the taxonomies and set them to hierarchical, but the Sub_Category tax seems to be relative to the Product Custom Post Type, NOT the Category custom taxonomy. Is there a way to do this? I saw a screenshot where someone was filling out a taxonomy form for a taxonomy they created and it had an option for PARENT but I could never get that to show up. How can I choose a PARENT Category taxonomy on my SubCategory taxonomy?

Read More

Here is my Category and SubCategory taxonomy code:

function create_productcategory_taxonomy() {
    $labels = array(
        'name' => _x( 'Categories', 'taxonomy general name' ),
        'singular_name' =>_x( 'Category', 'taxonomy singular name' ),
        'search_items' =>  __( 'Search Categories' ),
        'popular_items' => __( 'Popular Categories' ),
        'all_items' => __( 'All Categories' ),
        'parent_item' => null,
        'parent_item_colon' => null,
        'edit_item' => __( 'Edit Product Category' ),
        'update_item' => __( 'Update Product Category' ),
        'add_new_item' => __( 'Add New Product Category' ),
        'new_item_name' => __( 'New Product Category' ),
        'separate_items_with_commas' => __( 'Separate categories with commas' ),
        'add_or_remove_items' => __( 'Add or remove product categories' ),
        'choose_from_most_used' => __( 'Choose from the most used categories' )
        );

    register_taxonomy('tf_productcategory', 'tf_products', array (
        'label' => __('Product Category'),
        'labels' => $labels,
        'hierarchial' => true,
        'show_ui' => true,
        'query_var' => true,
        'rewrite' => array( 'slug' => 'product-category'),
        )); 
}

function create_product_subcategory_taxonomy() {
    $labels = array(
        'name' => _x( 'SubCategories', 'taxonomy general name' ),
        'singular_name' =>_x( 'SubCategory', 'taxonomy singular name' ),
        'search_items' =>  __( 'Search SubCategories' ),
        'popular_items' => __( 'Popular SubCategories' ),
        'all_items' => __( 'All SubCategories' ),
        'parent_item' => __( 'Main Category' ),
        'parent_item_colon' => ( 'Main Category:' ),
        'edit_item' => __( 'Edit Product SubCategory' ),
        'update_item' => __( 'Update Product SubCategory' ),
        'add_new_item' => __( 'Add New Product SubCategory' ),
        'new_item_name' => __( 'New Product SubCategory' ),
        'menu_name' => __( 'SubCategories' )
        );

    register_taxonomy('tf_productsubcategory', 'tf_products', array (
        'label' => __('Product SubCategory'),
        'labels' => $labels,
        'hierarchial' => true,
        'show_ui' => true,
        'query_var' => true,
        'rewrite' => array( 'slug' => 'product-subcategory'),
        )); 
}

add_action( 'init', 'create_productcategory_taxonomy', 0 );
add_action( 'init', 'create_product_subcategory_taxonomy', 0 );

Related posts

Leave a Reply

1 comment

  1. Hierarchical refers to the terms relation to each other not the taxonomies relation to another taxonomy. Non hierarchical taxonomies are like tags they don’t have parents or children. Hierarchical taxonomies means that the terms can have parents and children.

    taxonomy = category

    category terms:

    • blue products (parent term)

      • light blue products (child term)
      • dark blue products (child term)
    • red products (parent term)

      • dark red products (child term)
      • light red products (child term)

    Also in your code above change:

    'hierarchial' => true,
    

    To:

    'hierarchical' => true,