How to Register Custom Taxonomies in WordPress

How to Register Custom Taxonomies in WordPress

Custom taxonomies in WordPress allow you to organize and classify content beyond the default categories and tags. Follow these simple steps:

  1. Choose a Name: Pick a unique name that describes the taxonomy’s purpose, like "recipe-category" for categorizing recipes.
  2. Create Labels: Define user-friendly labels for your taxonomy:
Label Example
Singular Name Recipe Category
Plural Name Recipe Categories
Menu Name Recipe Categories
  1. Set Options: Decide if your taxonomy will be hierarchical (with sub-categories) or non-hierarchical, public or private, and visible in the WordPress admin area.
  2. Associate with Post Types: Specify which post types the taxonomy will be linked to, like custom post types or default posts and pages.
  3. Register the Taxonomy: Use the register_taxonomy() function to register your custom taxonomy with WordPress.

Once registered, you can:

  • Create Terms: Go to the WordPress dashboard, navigate to your taxonomy, and add new terms (categories).
  • Assign Terms: Associate the taxonomy terms with your posts or custom post types to categorize your content.
  • Display on Website: Use WordPress functions like wp_list_categories() or wp_get_object_terms() to display taxonomy terms and associated posts on your website. Alternatively, modify your theme templates to show the custom taxonomy.

Now that your custom taxonomy is set up, you can experiment with different taxonomy structures, labels, and options to find the best fit for your website. Integrate custom taxonomies into your digital strategy to improve content organization, filtering, and search functionality.

Requirements for Custom Taxonomies

WordPress Version

WordPress

Make sure your WordPress site is running the latest version. You can check for updates in the WordPress dashboard. Using the newest version ensures compatibility and security.

Helpful Plugins

Two plugins can simplify creating custom taxonomies:

  • Custom Post Type UI: This plugin provides an easy interface for creating custom post types and taxonomies. It’s simple to install and offers features to customize your taxonomies.
  • Pods: Pods is another popular plugin for creating custom post types and taxonomies. It’s flexible and user-friendly, making it a great option for beginners.

To install and activate these plugins:

  1. Log in to your WordPress dashboard
  2. Go to the "Plugins" menu
  3. Click "Add New"
  4. Search for the plugin (e.g. "Custom Post Type UI" or "Pods")
  5. Click "Install Now"
  6. Click "Activate" to enable the plugin
Plugin Description
Custom Post Type UI User-friendly interface for creating custom post types and taxonomies. Easy to install and customize.
Pods Flexible plugin for creating custom post types and taxonomies. Beginner-friendly and highly customizable.

Registering a Custom Taxonomy

Naming the Taxonomy

The first step is to choose a unique name for your taxonomy. The name should describe the taxonomy’s purpose. For example, if categorizing recipes, you could name it "recipe-category".

Taxonomy Labels

Next, create user-friendly labels for your taxonomy:

Label Example
Singular Name Recipe Category
Plural Name Recipe Categories
Menu Name Recipe Categories

Taxonomy Options

Configure the taxonomy options:

  • Hierarchical or non-hierarchical: Will your taxonomy have sub-categories (hierarchical) or not (non-hierarchical)?
  • Public or private: Will your taxonomy be publicly accessible or private?
  • Admin UI visibility: Will your taxonomy appear in the WordPress admin area?

Associating with Post Types

Specify which post types the taxonomy will be associated with, such as custom post types or default posts and pages.

Registering the Taxonomy

Use the register_taxonomy() function to register the custom taxonomy with WordPress:

function register_recipe_taxonomy() {
    $labels = array(
        'name' => __( 'Recipe Categories' ),
        'singular_name' => __( 'Recipe Category' ),
        'menu_name' => __( 'Recipe Categories' ),
    );
    $args = array(
        'labels' => $labels,
        'public' => true,
        'hierarchical' => true,
        'show_ui' => true,
        'show_admin_column' => true,
        'query_var' => true,
        'rewrite' => array( 'slug' => 'recipe-categories' ),
    );
    register_taxonomy( 'recipe-category', array( 'post' ), $args );
}
add_action( 'init', 'register_recipe_taxonomy' );

This code registers a custom taxonomy called "recipe-category" associated with the "post" post type.

sbb-itb-77ae9a4

After Registering the Taxonomy

Creating Taxonomy Terms

To create terms for your custom taxonomy:

  1. Go to the WordPress dashboard
  2. Click on the taxonomy name (e.g., "Recipe Categories") under the "Posts" or "Custom Post Types" menu
  3. Click "Add New"
  4. Enter the term name, slug, and description
  5. If your taxonomy is hierarchical, you can assign a parent term

Once you’ve created terms, you can assign them to your posts or custom post types. This allows you to categorize and filter your content using the custom taxonomy.

Displaying Taxonomies on Your Website

To display custom taxonomies on your website, you can use WordPress functions like wp_list_categories() or wp_get_object_terms(). These functions retrieve and display taxonomy terms and their associated posts.

Alternatively, you can modify your theme templates. For example, create a custom template file (e.g., taxonomy-recipe-category.php) to display the taxonomy terms and their associated posts.

Method Description
WordPress Functions Use wp_list_categories() or wp_get_object_terms() to retrieve and display taxonomy terms and posts.
Template Modifications Create a custom template file (e.g., taxonomy-recipe-category.php) to display the taxonomy terms and posts.

You can also use plugins like Custom Post Type UI or Toolset Types to display custom taxonomies on your website. These plugins provide a user-friendly interface for creating, managing, and displaying custom taxonomies.

Summary

Key Steps to Register a Custom Taxonomy

To register a custom taxonomy in WordPress, follow these simple steps:

  1. Choose a Name: Pick a unique name that describes the taxonomy’s purpose, like "recipe-category" for categorizing recipes.
  2. Create Labels: Define user-friendly labels for your taxonomy, such as:
Label Example
Singular Name Recipe Category
Plural Name Recipe Categories
Menu Name Recipe Categories
  1. Set Options: Decide if your taxonomy will be hierarchical (with sub-categories) or non-hierarchical, public or private, and visible in the WordPress admin area.
  2. Associate with Post Types: Specify which post types the taxonomy will be linked to, like custom post types or default posts and pages.
  3. Register the Taxonomy: Use the register_taxonomy() function to register your custom taxonomy with WordPress.

After Registration

Once registered, you can:

  • Create Terms: Go to the WordPress dashboard, navigate to your taxonomy, and add new terms (categories).
  • Assign Terms: Associate the taxonomy terms with your posts or custom post types to categorize your content.
  • Display on Website: Use WordPress functions like wp_list_categories() or wp_get_object_terms() to display taxonomy terms and associated posts on your website. Alternatively, modify your theme templates to show the custom taxonomy.

Further Customizations

Now that your custom taxonomy is set up, you can:

  • Experiment with different taxonomy structures, labels, and options to find the best fit for your website.
  • Integrate custom taxonomies into your digital strategy to improve content organization, filtering, and search functionality.

FAQs

How do I add custom taxonomies in WordPress?

1. Plan the taxonomy structure

Decide on a clear and descriptive name for your new taxonomy. The name should reflect the purpose of categorizing your content. For example, if you’re creating a taxonomy for movie genres, you could name it "movie-genres".

2. Add the taxonomy to your theme

Open your theme’s functions.php file and use the register_taxonomy() function to register your new taxonomy. This function allows you to set various options, such as:

  • Labels: Set user-friendly labels for the taxonomy (singular, plural, menu name, etc.).
  • Hierarchical or flat: Choose whether your taxonomy will have sub-categories (hierarchical) or not (flat).
  • Public or private: Decide if the taxonomy will be publicly accessible or private.
  • Admin visibility: Determine if the taxonomy will appear in the WordPress admin area.
  • Associated post types: Specify which post types the taxonomy will be linked to (e.g., posts, pages, custom post types).

Here’s an example of how to register a hierarchical, public taxonomy called "movie-genres" for the "post" post type:

function register_movie_genres_taxonomy() {
    $labels = array(
        'name' => 'Movie Genres',
        'singular_name' => 'Movie Genre',
        'menu_name' => 'Movie Genres',
    );
    $args = array(
        'labels' => $labels,
        'public' => true,
        'hierarchical' => true,
        'show_ui' => true,
        'show_admin_column' => true,
        'query_var' => true,
        'rewrite' => array( 'slug' => 'movie-genres' ),
    );
    register_taxonomy( 'movie-genres', array( 'post' ), $args );
}
add_action( 'init', 'register_movie_genres_taxonomy' );

3. Create taxonomy terms

After registering your taxonomy, you can create terms (categories) for it:

  • Go to the WordPress admin dashboard
  • Navigate to the new taxonomy (e.g., "Movie Genres") under the "Posts" menu
  • Click "Add New"
  • Enter the term name, slug, and description
  • If your taxonomy is hierarchical, you can assign a parent term

4. Assign terms to content

Once you’ve created terms, you can assign them to your posts or custom post types. This allows you to categorize and filter your content using the custom taxonomy.

5. Display the taxonomy on your website

To display your custom taxonomy on your website, you can:

  • Use WordPress functions like wp_list_categories() or wp_get_object_terms()
  • Modify your theme templates to create a custom template file (e.g., taxonomy-movie-genres.php) to display the taxonomy terms and associated posts

6. Test and refine

Before implementing your custom taxonomy on your live website, test it thoroughly on a staging environment. Identify and fix any issues, and refine the taxonomy structure and terms as needed.

Related posts

More WorDPRESS Tips, tutorials and Guides