WordPress Custom Taxonomies: Advanced Content Classification

When you create a Custom Post Type (such as Courses), relying solely on standard Categories and Tags often causes messy topic collisions. A Course belongs to a Department (e.g., Computer Science, Electrical Engineering) or an Academic Semester (Semester 1, Semester 6). WordPress allows developers to register tailored classification engines called Custom Taxonomies using register_taxonomy().

Hierarchical vs. Non-Hierarchical Taxonomies

When registering a taxonomy, the 'hierarchical' argument dictates how the taxonomy behaves in the editor:

  • Hierarchical ('hierarchical' => true): Functions exactly like Categories. Displays as a checklist in the editor and supports parent/child nested relationships (e.g. Faculty > Engineering > Computer Science).
  • Non-Hierarchical ('hierarchical' => false): Functions like Tags. Displays as a comma-separated freeform text input box with autocomplete, best for flat keywords (e.g. PHP8, React, Linux).

Complete PHP Code: Registering a "Department" Taxonomy

Attach a hierarchical department taxonomy to our course Custom Post Type:

// Register "Department" Taxonomy for "Course" CPT
add_action('init', 'veewom_register_department_taxonomy');

function veewom_register_department_taxonomy() {
    $labels = array(
        'name'              => _x('Departments', 'taxonomy general name', 'veewom'),
        'singular_name'     => _x('Department', 'taxonomy singular name', 'veewom'),
        'search_items'      => __('Search Departments', 'veewom'),
        'all_items'         => __('All Departments', 'veewom'),
        'parent_item'       => __('Parent Department', 'veewom'),
        'parent_item_colon' => __('Parent Department:', 'veewom'),
        'edit_item'         => __('Edit Department', 'veewom'),
        'update_item'       => __('Update Department', 'veewom'),
        'add_new_item'      => __('Add New Department', 'veewom'),
        'new_item_name'     => __('New Department Name', 'veewom'),
        'menu_name'         => __('Departments', 'veewom')
    );

    $args = array(
        'hierarchical'      => true, // true = Category style; false = Tag style
        'labels'            => $labels,
        'show_ui'           => true,
        'show_admin_column' => true, // Adds a sortable column to wp-admin post list!
        'query_var'         => true,
        'show_in_rest'      => true, // Required for Gutenberg panel support!
        'rewrite'           => array('slug' => 'department', 'with_front' => false)
    );

    // Attach to 'course' post type (can pass array of multiple post types)
    register_taxonomy('department', array('course'), $args);
}

Displaying Custom Taxonomy Terms in Theme Templates

Inside your theme's single-course.php template, display the attached department terms using get_the_term_list():

<?php
// Output formatted HTML list of attached departments
$departments = get_the_term_list( get_the_ID(), 'department', 'Department: ', ', ', '' );

if ( ! is_wp_error( $departments ) && ! empty( $departments ) ) {
    echo '<div class="course-dept-badge">' . $departments . '</div>';
}
?>

Template Hierarchy for Custom Taxonomies

When a visitor navigates to example.com/department/computer-science/, WordPress resolves the template in this exact order: 1) taxonomy-department-computer-science.php; 2) taxonomy-department.php; 3) taxonomy.php; 4) archive.php; 5) index.php.

Frequently Asked Questions (FAQ) - Student & Interview Review

Q1. Can a custom taxonomy be shared across multiple post types?

Yes! In `register_taxonomy()`, pass an array of post types: `array('course', 'post', 'product')`. This links the same department taxonomy across multiple content types.

Q2. What does 'show_admin_column' => true do?

It automatically creates a clean, sortable column in the WordPress admin list table (e.g. *All Courses* screen), showing which departments each course belongs to.

Q3. Can custom taxonomies have custom metadata (Term Meta)?

Yes! Using functions like `add_term_meta()` and `get_term_meta()`, developers can attach custom metadata (such as category header images or custom color pickers) to terms in the `wp_termmeta` table.

Explore Related WordPress Tutorials & Next Steps

Expand your CMS mastery by studying these tightly integrated WordPress development topics:

Custom Post Types (CPT)

Build relational data models linking CPTs with multiple taxonomies.

Categories & Tags Foundations

Core concepts of taxonomy hierarchy, terms, and URL slugs.

Template Hierarchy Guide

Render terms using taxonomy-{taxonomy}-{term}.php templates.

The Loop & WP_Query Mastery

Filter queries using advanced 'tax_query' relational parameters.

Share Share on Facebook Share on Twitter Share on LinkedIn Pin on Pinterest Share on Reddit

You may also like this!