Taxonomies in WordPress

Custom Taxonomies

Hierarchical Taxonomy

Hierarchical Taxonomies work like the default categories taxonomy and can have parent and child terms. Use the WP function register_taxonomy() with the init action hook in the functions.php theme file to define a custom Hierarchical Taxonomy.

function create_visual_art_hierarchical_taxonomy() {

    $labels = [
        'name'               => _x( 'Visual Art Categories', 'taxonomy general name', 'onetheme-text-domain'),
        'singular_name'      => _x( 'Visual Art Category', 'taxonomy singular name', 'onetheme-text-domain'),
        'search_items'       => __( 'Search Visual Art Categories', 'onetheme-text-domain'),
        'all_items'          => __( 'All Visual Art Categories', 'onetheme-text-domain'),
        'parent_item'        => __( 'Parent Visual Art Category', 'onetheme-text-domain'),
        'parent_item_colon'  => __( 'Parent Visual Art Category:', 'onetheme-text-domain'),
        'edit_item'          => __( 'Edit Visual Art Category', 'onetheme-text-domain'), 
        'update_item'        => __( 'Update Visual Art Category', 'onetheme-text-domain'),
        'add_new_item'       => __( 'Add New Visual Art Category', 'onetheme-text-domain'),
        'new_item_name'      => __( 'New Visual Art Category Name', 'onetheme-text-domain'),
        'menu_name'          => __( 'Visual Art Categories', 'onetheme-text-domain'),
    ];    

    // Register the 'visual_art_categories' taxonomy to the post type 'visual_art'.
    register_taxonomy('visual_art_taxonomy', ['visual_art'], [
        'hierarchical'       => true,
        'labels'             => $labels,
        'show_ui'            => true,
        'show_in_rest'       => true,
        'show_admin_column'  => true,
        'query_var'          => true,
        'rewrite'            => ['slug' => 'visual-art-categories'],
    ]);

}
add_action( 'init', 'create_visual_art_hierarchical_taxonomy', 0 );