Using WordPress ‘register_taxonomy()’ PHP function

The register_taxonomy() WordPress PHP function creates or modifies a taxonomy object.

Usage

register_taxonomy('genre', 'book', array(
    'hierarchical' => false,
    'label' => 'Genre',
));

Parameters

  • $taxonomy (string) – The taxonomy key. Must not exceed 32 characters and may only contain lowercase alphanumeric characters, dashes, and underscores.
  • $object_type (string|array) – Object type or array of object types with which the taxonomy should be associated.
  • $args (array|string) – Array or query string of arguments for registering a taxonomy.

More information

See WordPress Developer Resources: register_taxonomy()

Examples

Register a simple non-hierarchical taxonomy

add_action('init', 'create_genre_taxonomy');

function create_genre_taxonomy() {
    register_taxonomy('genre', 'book', array(
        'hierarchical' => false,
        'label' => 'Genre',
    ));
}

Register a hierarchical taxonomy

add_action('init', 'create_region_taxonomy');

function create_region_taxonomy() {
    register_taxonomy('region', 'city', array(
        'hierarchical' => true,
        'label' => 'Region',
    ));
}

Register a taxonomy with custom labels

add_action('init', 'create_film_genre_taxonomy');

function create_film_genre_taxonomy() {
    register_taxonomy('film_genre', 'film', array(
        'hierarchical' => false,
        'labels' => array(
            'name' => 'Film Genres',
            'singular_name' => 'Film Genre',
        ),
    ));
}

Register a taxonomy with custom REST API settings

add_action('init', 'create_video_game_genre_taxonomy');

function create_video_game_genre_taxonomy() {
    register_taxonomy('video_game_genre', 'video_game', array(
        'hierarchical' => false,
        'label' => 'Video Game Genre',
        'show_in_rest' => true,
        'rest_base' => 'game-genres',
    ));
}

Register a taxonomy with custom admin settings

add_action('init', 'create_author_taxonomy');

function create_author_taxonomy() {
    register_taxonomy('author', 'book', array(
        'hierarchical' => false,
        'label' => 'Author',
        'show_ui' => true,
        'show_in_menu' => true,
        'show_admin_column' => true,
    ));
}