WordPress Custom Taxonomies – Mastering Advanced WordPress Development Series

Mastering Advanced WordPress Development Series

WordPress taxonomies are a way to group and categorize your content. By default, WordPress provides two taxonomies: categories and tags. However, you may want to create your own custom taxonomies to organize your content in a more specific way.

In this tutorial, we’ll walk you through the process of creating your own custom taxonomies in WordPress. We’ll cover what taxonomies are, why you might want to use them, and how to create one step-by-step.

Part 1: What are WordPress custom taxonomies?

Before we dive into the details of creating a custom taxonomy, let’s first define what taxonomies are and why they’re useful.

Taxonomies are a way to group and categorize your content in WordPress. They allow you to organize your content in a way that makes sense for your specific use case. By default, WordPress provides two taxonomies: categories and tags. Categories are typically used to group your content into broad topics, while tags are used to describe specific topics or keywords.

Custom taxonomies are a way to extend the taxonomies that WordPress provides by default. With custom taxonomies, you can define your own taxonomies and the terms that they should have. This allows you to create a more specific and customized way to categorize your content.

Part 2: Creating a WordPress custom taxonomy

Now that we’ve covered what custom taxonomies are and why they’re useful, let’s dive into how to create one.

Step 1: Define your custom taxonomy

The first step to creating a custom taxonomy is to define what it will be. You’ll need to decide on a name for your custom taxonomy and the terms that it should have.

To define your custom taxonomy, you can add some code to your WordPress theme’s functions.php file. Here’s an example of what your code might look like:

/**
 * Registers a custom taxonomy for posts
 */
function custom_taxonomy() {

    // Set the labels for the custom taxonomy.
    $labels = array(
        'name'                       => esc_html__( 'Name', 'taxonomy general name' ), // The plural name for the taxonomy. Default is 'Categories'/'Tags'.
        'singular_name'              => esc_html__( 'Singular Name', 'taxonomy singular name' ), // The singular name for the taxonomy. Default is 'Category'/'Tag'.
        'search_items'               => esc_html__( 'Search Items' ), // The label for the search items field. Default is 'Search Categories'/'Search Tags'.
        'popular_items'              => esc_html__( 'Popular Items' ), // The label for the popular items field. Default is 'Popular Categories'/'Popular Tags'.
        'all_items'                  => esc_html__( 'All Items' ), // The label for the all items field. Default is 'All Categories'/'All Tags'.
        'parent_item'                => esc_html__( 'Parent Item' ), // The label for the parent item field. Default is 'Parent Category'/'Parent Tag'.
        'parent_item_colon'          => esc_html__( 'Parent Item:' ), // The label for the parent item field with colon. Default is 'Parent Category:'/'Parent Tag:'.
        'edit_item'                  => esc_html__( 'Edit Item' ), // The label for the edit item field. Default is 'Edit Category'/'Edit Tag'.
        'view_item'                  => esc_html__( 'View Item' ), // The label for the view item field. Default is 'View Category'/'View Tag'.
        'update_item'                => esc_html__( 'Update Item' ), // The label for the update item field. Default is 'Update Category'/'Update Tag'.
        'add_new_item'               => esc_html__( 'Add New Item' ), // The label for the add new item field. Default is 'Add New Category'/'Add New Tag'.
        'new_item_name'              => esc_html__( 'New Item Name' ), // The label for the new item name field. Default is 'New Category Name'/'New Tag Name'.
        'separate_items_with_commas' => esc_html__( 'Separate Items with Commas' ), // The label for the separate items with commas field. Default is 'Separate Categories with commas'/'Separate Tags with commas'.
        'add_or_remove_items'        => esc_html__( 'Add or Remove Items' ), // The label for the add or remove items field. Default is 'Add or remove categories'/'Add or remove tags'.
        'choose_from_most_used'      => esc_html__( 'Choose from the Most Used' ), // The label for the choose from the most used field. Default is 'Choose from the most used categories'/'Choose from the most used tags'.
        'not_found'                  => esc_html__( 'Not Found' ), // The label for the not found field. Default is 'No categories found'/'No tags found'.
        'no_terms'                   => esc_html__( 'No Items' ), // The label for the no terms field. Default is 'No categories'/'No tags'.
        'menu_name'                  => esc_html__( 'Menu Name' ), // The label for the menu name field. Default is the same as 'name'.
        'name_admin_bar'             => esc_html__( 'Name Admin Bar' ), // The label for the name displayed in the admin bar. Default is the same as 'singular_name'.
    );

    // Set the arguments for the custom taxonomy.
    $args = array(
        'labels'                => $labels, // (array) An array of labels for the taxonomy. Default is an empty array.
        'public'                => true, // (bool) Whether the taxonomy should be publicly queryable. Default is true.
        'show_ui'               => true, // (bool) Whether to show the taxonomy user interface. Default is true.
        'show_in_nav_menus'     => true, // (bool) Whether the taxonomy should be available in navigation menus. Default is true.
        'show_tagcloud'         => true, // (bool) Whether the taxonomy should be displayed in the tag cloud widget. Default is true.
        'meta_box_cb'           => null, // (callable) A callback function to render the meta box for this taxonomy. Default is null.
        'show_admin_column'     => false, // (bool) Whether to show the taxonomy in the admin column. Default is false.
        'description'           => '', // (string) A short description of the taxonomy. Default is an empty string.
        'hierarchical'          => false, // (bool) Whether the taxonomy should be hierarchical (like categories) or flat (like tags). Default is false.
        'update_count_callback' => '', // (callable) A callback function to update the term count for this taxonomy. Default is an empty string.
        'query_var'             => true, // (bool|string) Whether to allow querying of this taxonomy via the URL. Default is true.
        'rewrite'               => true, // (bool|array) Whether to allow URL rewriting for this taxonomy, and what the URL should look like. Default is true.
        'sort'                  => false, // (bool) Whether to sort the terms in this taxonomy. Default is false.
        'capabilities'          => array(), // (array) An array of capabilities for this taxonomy. Default is an empty array.
        'show_in_rest'          => false, // (bool) Whether the taxonomy should be available in the REST API. Default is false.
        'rest_base'             => null, // (string) The base URL for the REST API route. Default is null, which means the taxonomy name is used.
        'rest_controller_class' => 'WP_REST_Terms_Controller', // (string) The class to use for the REST API controller. Default is 'WP_REST_Terms_Controller'.
    );

    // Register the custom taxonomy.
    register_taxonomy( 'custom_taxonomy', array( 'post' ), $args );

}

// Add the custom taxonomy to the WordPress init hook
add_action( 'init', 'custom_taxonomy' );

In this code, replace "My Custom Taxonomy" with the name of your custom taxonomy, and replace "Custom Taxonomy" with the singular name of your custom taxonomy. You can also modify the other options in the $args array to fit your specific use case.

Step 2: Use your custom taxonomy

Now that you’ve created your custom taxonomy, you’re ready to use it in your WordPress site.

To add a term to your custom taxonomy, go to the WordPress dashboard and navigate to "Posts" > "Add New". In the right sidebar, you should see a section for your custom taxonomy. Click on "Add New Custom Taxonomy" to create a new term.

Conclusion:

In this tutorial, we’ve covered the basics of creating a custom taxonomy in WordPress. By defining a custom taxonomy and adding terms to it, you can create a more specific and customized way to categorize your content.

We covered the two main steps to creating a custom taxonomy in WordPress: defining the custom taxonomy and using it in your WordPress site. By following these steps, you can create your own custom taxonomy and add structured categorization to your WordPress site.


Stay in the loop with our web development newsletter - no spam, just juicy updates! Join us now. Join our web development newsletter to stay updated on the latest industry news, trends, and tips. No spam, just juicy updates delivered straight to your inbox. Don't miss out - sign up now!


We’ve tried our best to explain everything thoroughly, even though there’s so much information out there. If you found our writing helpful, we’d really appreciate it if you could buy us a coffee as a token of support.

Also, if you’re interested in learning more about WordPress, Javascript, HTML, CSS, and programming in general, you can subscribe to our MailChimp for some extra insights.

Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.