SponsoredHosinger Special Offer

WP+ WordPress Plugin Development

Build WordPress plugins from scratch: actions and filters, shortcodes, settings pages, custom post types, meta boxes, AJAX, REST endpoints, custom tables, cron and blocks.

Custom Taxonomies

WordPress Plugin Development Lesson 15 of 27 ~4 min read

Overview

A custom taxonomy is a way of grouping content — like categories and tags, but your own — registered with register_taxonomy() and attached to one or more post types.

Genres: a hierarchical taxonomy

<?php
function gdc_books_register_taxonomies() {
    register_taxonomy(
        'gdc_genre',
        array( 'gdc_book' ),
        array(
            'labels'            => array(
                'name'          => _x( 'Genres', 'taxonomy general name', 'gdc-books' ),
                'singular_name' => _x( 'Genre', 'taxonomy singular name', 'gdc-books' ),
                'add_new_item'  => __( 'Add New Genre', 'gdc-books' ),
                'search_items'  => __( 'Search Genres', 'gdc-books' ),
                'all_items'     => __( 'All Genres', 'gdc-books' ),
                'parent_item'   => __( 'Parent Genre', 'gdc-books' ),
            ),
            'hierarchical'      => true,      // Category-style, with parents and checkboxes.
            'public'            => true,
            'show_admin_column' => true,      // A Genre column in the Books list.
            'show_in_rest'      => true,      // Required for the block editor panel.
            'rewrite'           => array( 'slug' => 'genre' ),
        )
    );

    register_taxonomy(
        'gdc_book_author',
        array( 'gdc_book' ),
        array(
            'labels'            => array(
                'name'          => __( 'Book Authors', 'gdc-books' ),
                'singular_name' => __( 'Book Author', 'gdc-books' ),
            ),
            'hierarchical'      => false,     // Tag-style: type names freely.
            'show_admin_column' => true,
            'show_in_rest'      => true,
            'rewrite'           => array( 'slug' => 'book-author' ),
        )
    );
}
add_action( 'init', 'gdc_books_register_taxonomies' );
Hierarchical (true)Flat (false)
Behaves likeCategoriesTags
Editor UICheckbox list, with parentsType-and-add field
Good forA fixed structure: Fiction → FantasyOpen-ended labels: author names
Watch out: Taxonomy keys are limited to 32 characters and must not clash with core names. author, category, tag, type and year are reserved query variables — using them breaks archives in confusing ways. That is why this lesson uses gdc_book_author.

Taxonomy or custom field?

Use a taxonomy whenUse a custom field (post meta) when
Many posts share the valueThe value is unique to one post (ISBN)
Visitors should browse by itIt is only displayed, never browsed
You will query by it oftenYou rarely or never query by it

Taxonomy queries use indexed relationship tables and stay fast on large sites. That makes "genre" a perfect taxonomy and "ISBN" a perfect custom field.

Working with terms in code

<?php
// Assign genres to a book (replaces existing ones).
wp_set_object_terms( $book_id, array( 'fantasy', 'adventure' ), 'gdc_genre' );

// Read them.
$genres = get_the_terms( $book_id, 'gdc_genre' );
if ( $genres && ! is_wp_error( $genres ) ) {
    $names = wp_list_pluck( $genres, 'name' );
    echo esc_html( implode( ', ', $names ) );
}

// Create default terms on activation.
if ( ! term_exists( 'Fiction', 'gdc_genre' ) ) {
    wp_insert_term( 'Fiction', 'gdc_genre' );
}

Practice

  • Register both taxonomies and assign genres to your books.
  • Visit /genre/fantasy/ and see the archive (flush permalinks if it 404s).
  • Create default genres on activation with term_exists() and wp_insert_term().
Create a free account to save which lessons you've finished. Save my progress