How to Add Categories to a Custom Post Type in WordPress


Recently one of our user asked us if it was possible to add categories to a custom post type they have created. Categories are one of the built-in taxonomies in WordPress. By default they appear only for posts. However, in some scenarios you may want to share them with a custom post type as well. In this article, we will show you how to add categories to a custom post type in WordPress. We will also show you how to display multiple post types on your category archive page.

Adding categories to a custom post type

The Plugin Method

For our beginner level users, we recommend using Custom Post Type UI plugin to create custom post types. When using Custom Post Type UI plugin, you have the option to associate your custom post type to any built-in or custom taxonomy including categories.

First you need to install and activate the Custom Post Type UI plugin. For more details, see our step by step guide on how to install a WordPress plugin.

Upon installation, you need to visit CPT UI » Add/Edit Post Types to create a new custom post type or edit an existing custom post type you created with the plugin.

Editing post types with CPT UI plugin

Scroll down on the Advanced Options to the bottom and there you will see the Built in Taxnomies option. Check the box next to categories and save your custom post type.

Turn on categories for a Custom Post Type in WordPress

Don’t forget to click on the save post type button to store your settings.

Manually Adding Categories to a Custom Post Type

If you created your custom post type by adding the code in your theme’s functions.php file or a site-specific plugin, then you will have to modify the code to add category as supported taxonomy.

All you need to do is add this line in the arguments for your CPT.

  'taxonomies'  => array( 'category' ),  

It is likely that you may already have this line in the existing code for your CPT with some other custom taxonomy in it. If you do, then you just need to add a comma after that and add category, like this:

  'taxonomies'		  => array('topics', 'category' ),  

Here is a full example code where we have created a custom post type called movies with support for built-in categories.

  function custom_post_type() {    // Set UI labels for Custom Post Type  	$labels = array(  		'name'                => _x( 'Movies', 'Post Type General Name', 'twentythirteen' ),  		'singular_name'       => _x( 'Movie', 'Post Type Singular Name', 'twentythirteen' ),  		'menu_name'           => __( 'Movies', 'twentythirteen' ),  		'parent_item_colon'   => __( 'Parent Movie', 'twentythirteen' ),  		'all_items'           => __( 'All Movies', 'twentythirteen' ),  		'view_item'           => __( 'View Movie', 'twentythirteen' ),  		'add_new_item'        => __( 'Add New Movie', 'twentythirteen' ),  		'add_new'             => __( 'Add New', 'twentythirteen' ),  		'edit_item'           => __( 'Edit Movie', 'twentythirteen' ),  		'update_item'         => __( 'Update Movie', 'twentythirteen' ),  		'search_items'        => __( 'Search Movie', 'twentythirteen' ),  		'not_found'           => __( 'Not Found', 'twentythirteen' ),  		'not_found_in_trash'  => __( 'Not found in Trash', 'twentythirteen' ),  	);  	  // Set other options for Custom Post Type  	  	$args = array(  		'label'               => __( 'movies', 'twentythirteen' ),  		'description'         => __( 'Movie news and reviews', 'twentythirteen' ),  		'labels'              => $labels,  		'supports'            => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', 'custom-fields', ),  		'hierarchical'        => false,  		'public'              => true,  		'show_ui'             => true,  		'show_in_menu'        => true,  		'show_in_nav_menus'   => true,  		'show_in_admin_bar'   => true,  		'menu_position'       => 5,  		'can_export'          => true,  		'has_archive'         => true,  		'exclude_from_search' => false,  		'publicly_queryable'  => true,  		'capability_type'     => 'page',  		  		// This is where we add taxonomies to our CPT  		'taxonomies'          => array( 'category' ),  	);  	  	// Registering your Custom Post Type  	register_post_type( 'movies', $args );    }    /* Hook into the 'init' action so that the function  * Containing our post type registration is not   * unnecessarily executed.   */    add_action( 'init', 'custom_post_type', 0 );  

Displaying Multiple Post Types on Category Page

By default the category pages on your WordPress site will only display the default ‘Posts’ post type. To display your custom post types on the same category page as your default posts, you need to add this code into your theme’s functions.php or a site-specific plugin.

  add_filter('pre_get_posts', 'query_post_type');  function query_post_type($query) {    if( is_category() ) {      $post_type = get_query_var('post_type');      if($post_type)          $post_type = $post_type;      else          $post_type = array('nav_menu_item', 'post', 'movies'); // don't forget nav_menu_item to allow menus to work!      $query->set('post_type',$post_type);      return $query;      }  }  

Don’t forget to replace movies with the name of your own custom post type.

That’s all, we hope this article helped you add categories to your custom post type in WordPress. You can use the same methods to add tags to your custom post types as well.

If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Google+.

Seraphinite AcceleratorOptimized by Seraphinite Accelerator
Turns on site high speed to be attractive for people and search engines.