Home > Help Center > Applications > WordPress > WordPress Taxonomy

WordPress Taxonomy

Someone may say: I got tired from just reading about all these terms, definitions, methods, objects, themes, plugins, widgets, modules, templates, types, formats, excerpts, revisions ... and now: taxonomy.

What is taxonomy?

Taxonomies are the method of classifying content and data in WordPress. When you use a taxonomy you're grouping similar things together. The taxonomy refers to the sum of those groups.

Categories and tags are examples of default taxonomies which simply are a way to organize content.

Default WordPress Taxonomies

WordPress has four built-in taxonomies that you've probably used already.

Category

The category taxonomy lets you group posts together by sorting them into various categories. These categories can then be seen on the site by using /category/name types of URLs.

Tag

The post_tag taxonomy is similar to categories, but more free form. Tags can be made upon the fly, by simply typing them in. They can be seen on the site in the /tag/name types of URLs. Posts tend to have numerous tags, and they are generally displayed near posts or in the form of tag clouds.

Link Category

The link_category taxonomy lets you categorize your links. These tend to be used only internally, for organizational reasons, and are not usually exposed on the site itself. They are handy for defining groups of links to be displayed in sidebars and the like.

Post Formats

The post_format taxonomy is a piece of meta information that can be used by a theme to customize its presentation of a post. New post formats can't be created or added to the default existing ones.

Do You Need a Custom Post Type or Taxonomy

A twenty-pages site maintained by a single administrator probably does not need extra customizations and the default options are to be sufficient.

But what about the huge portal sites with tens of thousands of posts and hundreds of authors, working on dozens of different categories that are supported by a single WordPress installation.

Just try to imagine the chaos on the Posts screen.

Obviously, WordPress developers have once again done a great job, providing an extremely powerful way to group different items in all sorts of ways.

Custom taxonomy example

Now we are going to create a new custom post type books, then we'll create two taxonomies - genres and writers.

We'll create some new posts and we'll assign our new taxonomies to them. We'll also show you how to modify your template files to display the new posts.

You can copy/paste the same code from the examples in your WordPress site to test these features yourself in an easy and error-free way.

Create custom post type

Copy the code below and paste it in your theme functions.php file to register the custom post type books:

/**
 * Register a BOOKS post type.
 *
 * @link http://codex.wordpress.org/Function_Reference/register_post_type
 */
add_action( 'init', 'codex_book_init' );

function codex_book_init() {
  $labels = array(
    'name'               => _x( 'Books', 'post type general name', 'twentyseventeen' ),
    'singular_name'      => _x( 'Book', 'post type singular name', 'twentyseventeen' ),
    'menu_name'          => _x( 'Books', 'admin menu', 'twentyseventeen' ),
    'name_admin_bar'     => _x( 'Book', 'add new on admin bar', 'twentyseventeen' ),
    'add_new'            => _x( 'Add New', 'book', 'twentyseventeen' ),
    'add_new_item'       => __( 'Add New Book', 'twentyseventeen' ),
    'new_item'           => __( 'New Book', 'twentyseventeen' ),
    'edit_item'          => __( 'Edit Book', 'twentyseventeen' ),
    'view_item'          => __( 'View Book', 'twentyseventeen' ),
    'all_items'          => __( 'All Books', 'twentyseventeen' ),
    'search_items'       => __( 'Search Books', 'twentyseventeen' ),
    'parent_item_colon'  => __( 'Parent Books:', 'twentyseventeen' ),
    'not_found'          => __( 'No books found.', 'twentyseventeen' ),
    'not_found_in_trash' => __( 'No books found in Trash.', 'twentyseventeen' )
  );

  $args = array(
    'labels'             => $labels,
                'description'        => __( 'Description.', 'twentyseventeen' ),
    'public'             => true,
    'publicly_queryable' => true,
    'show_ui'            => true,
    'show_in_menu'       => true,
    'query_var'          => true,
    'rewrite'            => array( 'slug' => 'books' ),
    'capability_type'    => 'post',
    'has_archive'        => true,
    'hierarchical'       => false,
    'menu_position'      => null,
    'supports'           => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' )
  );

  register_post_type( 'books', $args );
}

Copy the code below and paste it in theme functions.php file to create new taxonomies genres and writers:

// Create two taxonomies, GENRES, and WRITERS for the custom_post_type BOOKS.

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

function create_book_taxonomies() {

  // Add new taxonomy GENRES, make it hierarchical (like categories).

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

  $args = array(
    'hierarchical'      => true,
    'labels'            => $labels,
    'show_ui'           => true,
    'show_admin_column' => true,
    'query_var'         => true,
    'rewrite'           => array( 'slug' => 'genres' ),
  );

  register_taxonomy( 'genres', array('books'), $args );

  // Add new taxonomy WRITERS, NOT hierarchical (like tags).

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

  $args = 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' => 'writers' ),
  );

  register_taxonomy( 'writers', array('books'), $args );
}

Notice the two last rows below each taxonomy:

register_taxonomy( 'genres', array('books'), $args );
register_taxonomy( 'writers', array('books'), $args );

If we add post as an array argument alongside books , the new taxonomies will be added to default WordPress Categories and Tags metaboxes on Add New and Edit post screen:

register_taxonomy( 'genres', array( 'post', 'books' ), $args );
register_taxonomy( 'writers', array( 'post', 'books' ), $args );

Wordpress metaboxes

So, if you want to extend the default taxonomies of WordPress you can register as many new taxonomies as you want, and add them as arguments to the register_taxonomy's array.

But in our example we want to assign our taxonomies to the custom post type books only:

Genres and writers meta boxes expanded

Save the file and refresh the administration - you must see a new menu item Books:

New custom post type books in the dashboard

Here are the new posts which we created within Books interface:

New posts in the Books interface

If we go to our home page we'll not see our new posts because our template file index.php is processing only the default post type post.

Copy and paste the below code in the function.php file to include to the main query the books post type also:

add_action( 'pre_get_posts', 'any_post_types_query' );
function any_post_types_query( $query ) {
    if ( is_home() && $query->is_main_query() )
        $query->set( 'post_type', array( 'post', 'books' ) );
    return $query;
}

Save the file and visit the home page - all new posts are now visible. But if you click on any title you are going to get … error 404 Not Found.

Yes, that's right. Because the template file for the single posts is single.php and it will only process the posts from the default post type.

If you've read our WordPress template hierarchy article, you'll guess you need to create a new template file compatible with the new post type.

If you think the new template file should be called single-books.php, you are absolutely right.

Create single-books.php file within the theme root folder and paste within the entire contents of the single.php template file.

Below the loop paste this code to display the corresponding taxonomy links:

Block of HTML code within PHP

Save the file.

If your site uses custom permalinks, you will need to flush your permalink structure after making changes to your taxonomies, or else you may see a Page Not Found error. Your permalink structure is automatically flushed when you visit Settings > Permalinks in your WordPress dashboard.

Now refresh the browser:

Flushing the permalink structure

If you click on the links you are going to visit the corresponding archive page:

Writers archive page

Genres archive page

Use default widgets for sidebar navigation

Go to Appearance > Widgets and drag Tag Cloud twice in the sidebar. Enter a title and from the dropdown menu select the new taxonomy as you see on the screenshot:

New taxonomy in the tag cloud widget

Now let's create a custom menu for the books post type. Go to Appearance > Menus create new menu and click Create Menu:

Custom menu for the Books post type

Click on the Books panel, click Select All and click Add to Menu:

Create books-menu

Click Save Menu.

Go back to Appearance > Widgets and drag Custom Menu in the Blog Sidebar. Enter a title and from the dropdown menu select the menu name:

Custom menu Books Posts in the sidebar

Save the menu and go to home page:

The finished sidebar

Conclusion

We hope this information will help you to start using more taxonomies and custom post types and to improve the user experience on your websites.

Still not finding what you're looking for?

Contact our support team with any additional questions or concerns.

Contact support