Web Design

How to add a custom taxonomy to ordinary posts in WordPress

WordPressの投稿にカスタムタクソノミー(分類)を追加

Hi there.
I’m Sato, a Web/App/UI designer.

In this article, I will write about How to add custom taxonomy (custom classification) to regular posts in WordPress.
This is useful when you want to categorize posts in WordPress and don’t want to manage them all together with “tags,” or when categories and tags are not enough.

What is Custom Taxonomy (Custom Classification)?

There are “categories” and “tags” that are provided for posts from the beginning, but these are additional functions (taxonomy) similar to those.

In many cases, it is used to give “custom post types” category or tag-like functionality.

It can be used like a category (one taxonomy per post) or like a tag (multiple taxonomies per post).

When using this function, create a “category name” and a “term” and use them.
A “category name” has the same meaning as a “category” or “tag,” and a “term” is each item in the category or tag.

Examples of category names and terms

Genre (classification name)
Japanese food (term)
Western food (term)
Chinese (term)

How to add a custom taxonomy

Add it by writing functions and parameters in functions.php.

Add it as a set with custom post types

For example, to add a custom classification as a set with a custom post type, write as follows


function create_post_type() {
  //custom post
  register_post_type(
    'cuisine', array(
      'label' => 'cuisine',
      'labels' => array(
        'name' => 'cuisine',
        'add_new' => 'add cuisine',
        'edit_item' => 'edit dish',
        'view_item' => 'view the dish',
        
        'enter_title_here' => 'Name of the dish'
      ),
      'hierarchical' => true,
      'menu_position' => 4,
      'has_archive' => true,
      'show_in_nav_menus' => true,
      'supports' => array(
        'title',
        'editor',
        'excerpt',
        'thumbnail',
        'custom-fields',
        'revisions',
        'page-attributes'
      )
    )
  );
  //custom taxonomy
  register_taxonomy(
    'cuisine_genre',
    'cuisine',
    array(
      'label' => 'cuisine category',
      'hierarchical' => true,
    )
  );
}
add_action( 'init', 'create_post_type' );

Custom taxonomies can be added to posts and fixed pages

I myself had the assumption that custom taxonomies are used with custom post types, but the other day I needed to add a third category for posts, after “categories” and “tags”, and I looked into it. I learned that custom taxonomies can be added to regular posts.

Or rather, to begin with, custom taxonomies are “a very powerful feature for grouping various items in various ways,” regardless of whether they are posts, fixed pages, custom posts, etc.

Click here for an explanation of custom taxonomy (official WordPress page).

The example text on the official page above shows an example of adding a taxonomy to a post (POST).

Now, let’s actually add a new taxonomy to a post.
You can add various parameters to control the details, but for now, we will use the minimum description that can be used normally.

Categorical usage


function register_custom_taxonomy_for_post() {
  register_taxonomy(
    'cuisine_genre', /* taxonomy name */
    'post', /* 'post' if adding to a post, 'page' if a fixed page, custom post type name if a custom post type */
    array(
      'label' => 'cuisine genre', /* display name for classification */
      'hierarchical' => true, /* hierarchical or not */
      'show_admin_column' => true, /* whether to show the columns of the classification on the admin post list page */
    )
  );
}
add_action('init', 'register_custom_taxonomy_for_post');

In the case of categorical usage, there will be cases such as “parent category > child category > grandchild category”, so the value of the “hierarchical” parameter should be set to “true”.

Also, the value of the “show_admin_column” parameter should also be set to “true” since the columns of the added categories are not displayed in the post list screen of the administration page by default.

For tag-like usage


function register_custom_taxonomy_for_post() {
  register_taxonomy(
    'price_range', /* taxonomy name */
    'post', /* 'post' if adding to post, 'page' if fixed page, custom post type name if custom post type */
    array(
      'label' => 'price range', /* display name for classification */
      'hierarchical' => false, /* hierarchical or not */
      'show_admin_column' => true, /* whether to show the columns of the classification in the admin post list page */
      'update_count_callback' => '_update_post_term_count', /* required for tag-like usage */
    )
  );
}
add_action('init', 'register_custom_taxonomy_for_post');

The basic description is the same as for categorical usage.
However, the “hierarchical” parameter should be set to “false” because tag-like usage does not require a parent-child relationship like a category.

Also, add the parameter “update_count_callback”, which was not necessary when using it like a category, and set its value to “_update_post_term_count”.
Note that without this setting, multiple tags that should have been set as comma-separated from the post screen will be treated as a single string that is combined together.

Implementation of filtering by added classifications in the list of posts on the admin screen

To enable filtering by added categories in the list of posts in the admin panel, add the following description to functions.php.


function add_term_dropdown( $post_type ) {
  if($post_type == 'post') { /* post, page, custom post type name, etc. */
    $term_slug = get_query_var('cuisine_genre'); /* category name */
    wp_dropdown_categories(array(
      'show_option_all' => __('cuisine_genre', 'my_theme'),
      'selected' => $term_slug,
      'name' => 'cuisine_genre', /* classification name */
      'taxonomy' => 'cuisine_genre', /* classification name */
      'value_field' => 'slug',
    ));
  }
}
add_action('restrict_manage_posts', 'add_term_dropdown', 10, 3);

Well, in this article, I wrote about How to add custom taxonomy (custom classification) to regular posts in WordPress.
How was it?
I myself learned some eye-opening lessons this time.

I would be happy if it could be of some help to you.
See you next time.