Manually Adding Categories to a Custom Post Type with Code
If you created your custom post type by adding the code in your theme’s functions.php file, a site-specific plugin, or in a code snippets plugin, then you will have to modify the code to add category as supported taxonomy.
Here is a full example of code where we have created a custom post type called ‘Movies’ with support for built-in categories.
functioncustom_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','show_in_rest'=> true,// This is where we add taxonomies to our CPT'taxonomies'=>array('category'),);// Registering your Custom Post Typeregister_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 );
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 file or in a code snippets plugin:
add_filter('pre_get_posts','query_post_type');functionquery_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;}}