Mastering Custom Taxonomies in WordPress Themes

Posted on 19th June 2023

In this article we’re going to explore custom taxonomies in WordPress themes. We’ll cover the following topics:

  • What are custom taxonomies?
  • Why use custom taxonomies in WordPress?
  • How to create custom taxonomies in WordPress?

What are custom taxonomies?

Custom taxonomies are a way to group similar content together. For example, you might have a custom taxonomy for “Authors” that groups all of the authors on your website together.

Custom taxonomies can be used for any type of content, not just posts. You can use custom taxonomies to group:

  • Posts
  • Pages
  • Comments
  • Users

Why use custom taxonomies in WordPress?

Custom taxonomies are a powerful way to organize your content. They give you more control over your content and make it easier for people to find what they’re looking for.

Custom taxonomies can also be used to create custom navigation menus. For example, you could create a custom taxonomy for “Categories” and then use that to create a custom navigation menu.

How to create custom taxonomies in WordPress?

Creating custom taxonomies in WordPress is a two-step process:

  1. Register the taxonomy
  2. Associate the taxonomy with a post type

We’ll cover both of these steps in detail below.

Step 1: Register the taxonomy

The first step is to register the taxonomy. You can do this by adding the following code to your functions.php file:

‘Custom Taxonomies’,
‘singular_name’ => ‘Custom Taxonomy’,
‘add_new’ => ‘Add New Custom Taxonomy’,
‘add_new_item’ => ‘Add New Custom Taxonomy’,
‘edit_item’ => ‘Edit Custom Taxonomy’,
‘new_item’ => ‘New Custom Taxonomy’,
‘view_item’ => ‘View Custom Taxonomy’,
‘search_items’ => ‘Search Custom Taxonomies’,
‘not_found’ => ‘No custom taxonomies found’,
‘not_found_in_trash’ => ‘No custom taxonomies found in Trash’,
);

$args = array(
‘labels’ => $labels,
‘public’ => true,
‘hierarchical’ => true,
‘show_ui’ => true,
‘show_in_menu’ => true,
‘show_in_nav_menus’ => true,
‘show_in_admin_bar’ => true,
‘menu_position’ => 5,
‘menu_icon’ => ‘dashicons-admin-customizer’,
‘can_export’ => true,
‘has_archive’ => true,
‘exclude_from_search’ => false,
‘publicly_queryable’ => true,
‘capability_type’ => ‘post’,
);

register_taxonomy( ‘custom_taxonomy’, ‘post_type’, $args );
}
add_action( ‘init’, ‘my_custom_taxonomy’ );
?>
In the code above, we’ve registered a custom taxonomy called “Custom Taxonomies”. This taxonomy will be used to group similar content together.

Step 2: Associate the taxonomy with a post type

The next step is to associate the taxonomy with a post type. You can do this by adding the following code to your functions.php file:

true,
‘labels’ => array(
‘name’ => ‘Custom Taxonomies’,
‘singular_name’ => ‘Custom Taxonomy’,
‘search_items’ => ‘Search Custom Taxonomies’,
‘popular_items’ => ‘Popular Custom Taxonomies’,
‘all_items’ => ‘All Custom Taxonomies’,
‘parent_item’ => ‘Parent Custom Taxonomy’,
‘parent_item_colon’ => ‘Parent Custom Taxonomy:’,
‘edit_item’ => ‘Edit Custom Taxonomy’,
‘update_item’ => ‘Update Custom Taxonomy’,
‘add_new_item’ => ‘Add New Custom Taxonomy’,
‘new_item_name’ => ‘New Custom Taxonomy’,
‘separate_items_with_commas’ => ‘Separate custom taxonomies with commas’,
‘add_or_remove_items’ => ‘Add or remove custom taxonomies’,
‘choose_from_most_used’ => ‘Choose from the most used custom taxonomies’,
),
‘show_ui’ => true,
‘show_tagcloud’ => true,
‘query_var’ => true,
‘rewrite’ => true,
) );
}
add_action( ‘init’, ‘my_custom_taxonomy_init’ );
?>
In the code above, we’ve associated the “Custom Taxonomies” taxonomy with the “post_type” post type. This will allow us to group similar content together.

That’s it! You’ve now learned how to create custom taxonomies in WordPress.

What is a Custom Taxonomy?

A custom taxonomy is a way to group content together that wouldn’t normally fit into the default WordPress categories or tags. WordPress comes with a few built-in taxonomies like post formats, link categories, and post tags, but you can also create your own. Custom taxonomies are usually created to group together similar content that falls outside of the default WordPress categories. For example, you might want to create a custom taxonomy for movie genres, or for product types on an eCommerce site.

Creating a Custom Taxonomy

There are two parts to creating a custom taxonomy: registering the taxonomy, and then assigning it to a post type.

Registering the Taxonomy

First, you need to register the taxonomy using the register_taxonomy() function. This function takes 4 arguments:

The name of the taxonomy. This should be a unique name that is all lowercase with no spaces.

The name of the post type or array of post types that the taxonomy should be assigned to.

An array of options for the taxonomy.

An array of labels for the taxonomy.

Here’s an example of how to register a custom taxonomy for movie genres:

function movie_genres() {

$labels = array(

‘name’ => ‘Movie Genres’,

‘singular_name’ => ‘Movie Genre’,

‘search_items’ => ‘Search Movie Genres’,

‘all_items’ => ‘All Movie Genres’,

‘parent_item’ => ‘Parent Movie Genre’,

‘parent_item_colon’ => ‘Parent Movie Genre:’,

‘edit_item’ => ‘Edit Movie Genre’,

‘update_item’ => ‘Update Movie Genre’,

‘add_new_item’ => ‘Add New Movie Genre’,

‘new_item_name’ => ‘New Movie Genre Name’,

‘menu_name’ => ‘Movie Genres’,

);

$args = array(

‘labels’ => $labels,

‘hierarchical’ => true,

‘show_ui’ => true,

‘show_admin_column’ => true,

‘query_var’ => true,

‘rewrite’ => array( ‘slug’ => ‘movie-genre’ ),

);

register_taxonomy( ‘movie_genre’, ‘movies’, $args );

}
add_action( ‘init’, ‘movie_genres’ );

Assigning a Taxonomy to a Post Type

Once the taxonomy is registered, you need to assign it to a post type. You can do this using the register_taxonomy_for_object_type() function. This function takes 2 arguments:

The name of the taxonomy.

The name of the post type or array of post types that the taxonomy should be assigned to.

Here’s an example of how to assign the movie_genre taxonomy to the movies post type:

register_taxonomy_for_object_type( ‘movie_genre’, ‘movies’ );

Displaying a Custom Taxonomy

Now that you’ve registered and assigned your custom taxonomy, you can start using it. To display the terms of a custom taxonomy, you can use either the get_terms() or wp_list_terms() functions.

The get_terms() function returns an array of term objects, while the wp_list_terms() function displays the terms as a list.

Here’s an example of how to use the get_terms() function to get all the terms of the movie_genre taxonomy:

$terms = get_terms( ‘movie_genre’, array(

‘hide_empty’ => false,

) );

foreach ( $terms as $term ) {

echo ‘

‘ . $term->name . ‘

‘;

}

And here’s an example of how to use the wp_list_terms() function to display the terms of the movie_genre taxonomy as a list:

wp_list_terms( ‘movie_genre’, array(

‘title_li’ => ”,

) );

Adding Terms to a Taxonomy

To add a new term to a taxonomy, you can use the wp_insert_term() function. This function takes 3 arguments:

The name of the taxonomy.

An array of term data.

An array of arguments.

Here’s an example of how to use the wp_insert_term() function to add a new term to the movie_genre taxonomy:

wp_insert_term(

‘Comedy’,

‘movie_genre’,

array(

‘slug’ => ‘comedy’,

‘description’ => ‘A movie genre that is primarily focused on humor.’,

)

);

Updating Terms

To update an existing term, you can use the wp_update_term() function. This function takes 3 arguments:

The ID of the term to update.

The name of the taxonomy.

An array of term data.

Here’s an example of how to use the wp_update_term() function to update a term in the movie_genre taxonomy:

wp_update_term(

1,

‘movie_genre’,

array(

‘name’ => ‘Comedy’,

‘slug’ => ‘comedy’,

‘description’ => ‘A movie genre that is primarily focused on humor.’,

)

);

Deleting Terms

To delete a term from a taxonomy, you can use the wp_delete_term() function. This function takes 2 arguments:

The ID of the term to delete.

The name of the taxonomy.

Here’s an example of how to use the wp_delete_term() function to delete a term from the movie_genre taxonomy:

wp_delete_term( 1, ‘movie_genre’ );

Conclusion

Custom taxonomies are a powerful way to group content together in WordPress. They can be used to create anything from a simple tag cloud to a complex product catalog. In this article, we’ve covered everything you need to know about custom taxonomies in WordPress, from registering and assigning them to post types, to displaying and managing terms.