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:
- Define the Taxonomy: Use theÂ
register_taxonomy() function to define the taxonomy. Specify the name, labels, and settings for the taxonomy. - 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.
- 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):
✅ 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:
✅ 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.