Add Custom Taxonomies to Custom Post Types in WordPress

Add Custom Taxonomies to Custom Post Types

You just need a function to add Custom Taxonomies to Custom Post Types. In previous article we made WordPress default taxonomies called Category & Tag to support our CPTs. Here we are going to create custom taxonomies for those CPTs.

In earlier two articles of CPTs series, we learnt to create Custom Post Types and used WP categories & tags taxonomies in them. This article is 3rd continuous part of CPTs series:

WordPress Categories and Tags taxonomies to CPT

Custom taxonomies for custom post type content

Adding custom fields to CPT

Add Custom Taxonomies to Custom Post Types

In your functions.php or plugin specific file, write the following function. 

Function register_taxonomy()

The register_taxonomy() function does all the things for you. It has a number of parameters to pass which you should read for better understanding.

Here we are creating two taxonomies for book post type. First is Genre which is a hierarchical taxonomy that follows parent-child relationship and is same as Category for Post post type. Another is Book Tag which is multifaceted (non hierarchical) taxonomy.

Parameters:

  • show_ui: Pass true to generate a default UI for managing this taxonomy.
  • show_admin_column: This is used to  enable automatic creation of taxonomy columns on associated post-types table.
  • query_var: Allows uses of default WP_Query if true passed.
  • with_front: We don’t want to display category base before taxonomy slug. If fiction is a term created in genre taxonomy, the URL slug will be: /genre/fiction/
  • update_count_callback: To ensure that the custom taxonomy behaves like a tag, you must add the option ‘update_count_callback’ => ‘_update_post_term_count’.

The first argument passed to register_taxonomy() function is the name of the taxonomy object. Please note that WordPress has a lot reserved terms which can be seen when reading about the function. Don’t use any reserved term while providing slug or arguments to add custom taxonomies.

Second argument is array / string with name of custom post type object. You can add more post types here including default WordPress post types too.

Hook into init to Add Custom Taxonomies

The function we created above needs to hook into init action. check our initial  my_posttype_and_taxonomy() function where we have commented out call to my_cpt_taxonomies() at line no #34. Remove comment syntax (two trailing slashes) from there and you’re done.

Refresh Permalink to Create Custom Taxonomy Slug

Before you could start working with custom taxonomies or  can add custom fields for these taxonomies, you need to refresh permalink to let them work perfectly. Go to Settings -> Permalinks from dashboard menus and just click Save changes button once. Now check if Genres and Book Tags sub-menus are there under Books menu.

So here is all the way to create or add custom taxonomies for custom post types in WordPress. In next series we will see how to create custom fields for these taxonomies/ CPTs.

You Might Interested In