How to Add Categories to a Custom Post Type in WordPress

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. You can now visit a category archive page and it will display your entries from your custom post type.

We hope this article helped you learn how to 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. See our guide: categories vs. tags to learn more.