0
0
WordpressHow-ToBeginner · 4 min read

How to Create Custom Taxonomy in WordPress Easily

To create a custom taxonomy in WordPress, use the register_taxonomy() function inside a hook like init. This lets you add new ways to group content, such as custom categories or tags, by defining labels and settings.
📐

Syntax

The register_taxonomy() function registers a new taxonomy in WordPress. It requires three main parts:

  • taxonomy: a unique string ID for your taxonomy.
  • object_type: the post type(s) this taxonomy applies to (like 'post' or 'page').
  • args: an array of settings including labels and behavior.
php
register_taxonomy( string $taxonomy, string|array $object_type, array $args = array() );
💻

Example

This example creates a custom taxonomy called 'Genre' for posts. It shows how to add it in your theme's functions.php or a plugin.

php
<?php
function create_genre_taxonomy() {
    $labels = array(
        'name' => 'Genres',
        'singular_name' => 'Genre',
        'search_items' => 'Search Genres',
        'all_items' => 'All Genres',
        '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, // like categories
        'labels' => $labels,
        'show_ui' => true,
        'show_admin_column' => true,
        'query_var' => true,
        'rewrite' => array('slug' => 'genre'),
    );

    register_taxonomy('genre', array('post'), $args);
}
add_action('init', 'create_genre_taxonomy');
Output
A new 'Genres' taxonomy appears in the WordPress admin sidebar under Posts, allowing you to assign genres to posts.
⚠️

Common Pitfalls

Common mistakes when creating custom taxonomies include:

  • Not hooking register_taxonomy() to init, causing it not to load properly.
  • Using a taxonomy name that conflicts with existing taxonomies or reserved words.
  • Forgetting to set hierarchical correctly: true for category-like, false for tag-like taxonomies.
  • Not flushing rewrite rules after registering, so URLs may 404.

To flush rewrite rules, visit Settings > Permalinks in the admin and click Save.

php
<?php
// Wrong: calling register_taxonomy outside init hook
register_taxonomy('genre', 'post', array('hierarchical' => true));

// Right: inside init hook
add_action('init', function() {
    register_taxonomy('genre', 'post', array('hierarchical' => true));
});
📊

Quick Reference

Summary tips for creating custom taxonomies:

  • Use register_taxonomy() inside init hook.
  • Choose a unique taxonomy name (lowercase, no spaces).
  • Set hierarchical to true for category style, false for tag style.
  • Define clear labels for admin UI.
  • Flush rewrite rules after adding new taxonomies.

Key Takeaways

Always register custom taxonomies inside the 'init' hook to ensure proper loading.
Use 'hierarchical' true for category-like taxonomies and false for tag-like taxonomies.
Choose unique, lowercase taxonomy names to avoid conflicts.
Flush rewrite rules after registering new taxonomies by saving permalinks settings.
Provide clear labels to make the taxonomy user-friendly in the admin area.