How to Create Custom Taxonomies in WordPress

Updated November 15th, 2017
Updated November 15th, 2017
Share this post:
Share on facebook
Share on twitter
Share on reddit
Share on linkedin
Share on pinterest
Share on pocket
Share on email
Share on print

The newly introduced feature of creating your own taxonomy (grouping mechanism for posts and custom post types) in WordPress is quite the useful. But it can be tricky to use, especially for someone new to WordPress. So to make things a little easier here’s a walk through of how to create custom taxonomies in WordPress.

Creating a Taxonomy

Select the Settings tab from the toolbar and proceed to Custom Taxonomies. Name your taxonomy. Bear in mind that the name must not contain weird characters and must always be in lower case. Next follows the nature of whether you like your taxonomy to be hierarchical or in form of tags. Hierarchical implies you can add a parent and a child. Choose true for a hierarchical system or else select false.

The next step is to affiliate the created taxonomy with a post and the last step is to a have an option of automatically adding the words or not. To elaborate, let’s use an example of creating a taxonomy named Topics. The WordPress site must be told how it should translate user interface for the Topics.

Click the Add Taxonomy button once you are done with providing the translations. A custom created taxonomy will be displayed under Posts and will have an interface similar to Categories and Tags. The Taxonomy field will also be displayed in the edit post area.

Manually Creating a Taxonomy

To manually create a Taxonomy, syntax similar to the following is used. The example is of a hierarchical taxonomy. In the syntax that follows, the difference between the two codes is quite evident. In the hierarchical structure the hierarchical argument is true for category-like taxonomies and false for tag-like taxonomies. Also, for a non-hierarchical structure the parent_item and parent_item_colon argument are added with “null” implying that nothing will be shown in the user interface.

Hierarchical Taxonomy


//hook into the init action and call create_book_taxonomies when it fires
add_action( 'init', 'create_topics_hierarchical_taxonomy', 0 );

//create a custom taxonomy name it topics for your posts

function create_topics_hierarchical_taxonomy() {

// Add new taxonomy, make it hierarchical like categories
//first do the translations part for GUI

  $labels = array(
    'name' => _x( 'Topics', 'taxonomy general name' ),
    'singular_name' => _x( 'Topic', 'taxonomy singular name' ),
    'search_items' =>  __( 'Search Topics' ),
    'all_items' => __( 'All Topics' ),
    'parent_item' => __( 'Parent Topic' ),
    'parent_item_colon' => __( 'Parent Topic:' ),
    'edit_item' => __( 'Edit Topic' ), 
    'update_item' => __( 'Update Topic' ),
    'add_new_item' => __( 'Add New Topic' ),
    'new_item_name' => __( 'New Topic Name' ),
    'menu_name' => __( 'Topics' ),
  ); 	

// Now register the taxonomy

  register_taxonomy('topics',array('post'), array(
    'hierarchical' => true,
    'labels' => $labels,
    'show_ui' => true,
    'show_admin_column' => true,
    'query_var' => true,
    'rewrite' => array( 'slug' => 'topic' ),
  ));

}

Non-hierarchical custom taxonomy

Add this code in your theme functions.php or in a site-specific plugin:

//hook into the init action and call create_topics_nonhierarchical_taxonomy when it fires

add_action( 'init', 'create_topics_nonhierarchical_taxonomy', 0 );

function create_topics_nonhierarchical_taxonomy() {

// Labels part for the GUI

  $labels = array(
    'name' => _x( 'Topics', 'taxonomy general name' ),
    'singular_name' => _x( 'Topic', 'taxonomy singular name' ),
    'search_items' =>  __( 'Search Topics' ),
    'popular_items' => __( 'Popular Topics' ),
    'all_items' => __( 'All Topics' ),
    'parent_item' => null,
    'parent_item_colon' => null,
    'edit_item' => __( 'Edit Topic' ), 
    'update_item' => __( 'Update Topic' ),
    'add_new_item' => __( 'Add New Topic' ),
    'new_item_name' => __( 'New Topic Name' ),
    'separate_items_with_commas' => __( 'Separate topics with commas' ),
    'add_or_remove_items' => __( 'Add or remove topics' ),
    'choose_from_most_used' => __( 'Choose from the most used topics' ),
    'menu_name' => __( 'Topics' ),
  ); 

// Now register the non-hierarchical taxonomy like tag

  register_taxonomy('topics','post',array(
    'hierarchical' => false,
    'labels' => $labels,
    'show_ui' => true,
    'show_admin_column' => true,
    'update_count_callback' => '_update_post_term_count',
    'query_var' => true,
    'rewrite' => array( 'slug' => 'topic' ),
  ));
}
Share this post:
Share on facebook
Share on twitter
Share on reddit
Share on linkedin
Share on pinterest
Share on pocket
Share on email
Share on print
Share on facebook
Share on twitter
Share on linkedin
Share on pinterest
Share on email

Responses

Your email address will not be published.

WPLearningLab

Warning: Use of undefined constant WPBF_CHILD_THEME_URI - assumed 'WPBF_CHILD_THEME_URI' (this will throw an Error in a future version of PHP) in /home/customer/www/wplearninglab.com/public_html/wp-content/themes/buddyboss-theme-child/functions.php on line 108

Warning: Use of undefined constant WPBF_CHILD_VERSION - assumed 'WPBF_CHILD_VERSION' (this will throw an Error in a future version of PHP) in /home/customer/www/wplearninglab.com/public_html/wp-content/themes/buddyboss-theme-child/functions.php on line 108