Custom Taxonomy in WordPress (custom post type)

What Are Taxonomies in WordPress?

Taxonomy is a way of classifying and organizing content. By default, WordPress provides three built-in taxonomies: categories, tags, and post formats. However, these may not always be sufficient to meet the specific requirements of every website.

What is Custom Taxonomy? Custom taxonomy, as the name suggests, allows you to create and define your own taxonomies. It provides a flexible and efficient solution for organizing content beyond the default taxonomies. With custom taxonomy, you can create custom labels, hierarchies, and classifications that are tailored to your website’s unique needs.

Implementing Custom Taxonomy in WordPress: To implement custom taxonomy in WordPress, you can utilize a combination of built-in functions and custom code. Here’s a step-by-step guide to get you started:

  1. Define the Taxonomy: Use the register_taxonomy() function to define the taxonomy. Specify the name, labels, and settings for the taxonomy.
  2. Assign Taxonomy to Content: After defining the taxonomy, you can assign it to different content types such as posts, pages, or custom post types. This ensures that the taxonomy becomes available for classification and organization.
  3. Displaying Taxonomy on the Frontend: To showcase the custom taxonomy on your website’s frontend, you need to modify your theme or create custom templates. Utilize functions like get_terms() or get_the_terms() to retrieve and display the taxonomy terms associated with a specific content item.

STEP 1: Register the Custom Post Type – “Books”

Add this code to your theme’s functions.php (or a small plugin):

function create_books_cpt() {

$labels = array(
‘name’ => _x(‘Books’, ‘Post Type General Name’),
‘singular_name’ => _x(‘Book’, ‘Post Type Singular Name’),
‘menu_name’ => __(‘Books’),
‘name_admin_bar’ => __(‘Book’),
‘archives’ => __(‘Book Archives’),
‘attributes’ => __(‘Book Attributes’),
‘parent_item_colon’ => __(‘Parent Book:’),
‘all_items’ => __(‘All Books’),
‘add_new_item’ => __(‘Add New Book’),
‘add_new’ => __(‘Add New’),
‘new_item’ => __(‘New Book’),
‘edit_item’ => __(‘Edit Book’),
‘update_item’ => __(‘Update Book’),
‘view_item’ => __(‘View Book’),
‘view_items’ => __(‘View Books’),
‘search_items’ => __(‘Search Book’),
);

$args = array(
‘label’ => __(‘Books’),
‘description’ => __(‘A collection of books and their details’),
‘labels’ => $labels,
‘menu_icon’ => ‘dashicons-book’, // WordPress book icon
‘supports’ => array(‘title’, ‘editor’, ‘thumbnail’, ‘excerpt’),
‘hierarchical’ => false,
‘public’ => true,
‘show_ui’ => true,
‘show_in_menu’ => true,
‘show_in_rest’ => true, // important for Gutenberg & REST API
‘has_archive’ => true,
‘rewrite’ => array(‘slug’ => ‘books’),
);

register_post_type(‘book’, $args);
}
add_action(‘init’, ‘create_books_cpt’);

✅ This creates the “Books” post type in your WordPress Dashboard.

STEP 2: Register the Custom Taxonomy – “Genres”

Now, we’ll add a taxonomy to group the books by genre (like Fiction, Thriller, Sci-Fi, etc.).

Add this below your CPT function:

function create_genre_taxonomy() {
$labels = array(
‘name’ => _x(‘Genres’, ‘taxonomy general name’),
‘singular_name’ => _x(‘Genre’, ‘taxonomy singular name’),
‘search_items’ => __(‘Search Genres’),
‘all_items’ => __(‘All Genres’),
‘parent_item’ => __(‘Parent Genre’),
‘parent_item_colon’ => __(‘Parent Genre:’),
‘edit_item’ => __(‘Edit Genre’),
‘update_item’ => __(‘Update Genre’),
‘add_new_item’ => __(‘Add New Genre’),
‘new_item_name’ => __(‘New Genre Name’),
‘menu_name’ => __(‘Genres’),
);

$args = array(
‘hierarchical’ => true, // true = like categories
‘labels’ => $labels,
‘show_ui’ => true,
‘show_admin_column’ => true,
‘query_var’ => true,
‘show_in_rest’ => true, // allows editing in block editor
‘rewrite’ => array(‘slug’ => ‘genre’),
);

register_taxonomy(‘genre’, array(‘book’), $args);
}
add_action(‘init’, ‘create_genre_taxonomy’);

✅ Now you’ll see “Genres” appear under the “Books” menu in your admin sidebar.

You can create Genre terms (e.g., “Fiction”, “Non-Fiction”, “Romance”, etc.) and assign them to each book.

Share on Facebook Share on Twitter