Add a Custom Post Type to the Category Page in WordPress
Jul 10, 2020 | WordPress
function custom_post_type_cat_filter($query) {
if ( !is_admin() && $query->is_main_query() ) {
if ($query->is_category()) {
$query->set( 'post_type', array( 'post', 'xxx' ) );
}
}
}
add_action('pre_get_posts','custom_post_type_cat_filter');
Custom post types are excluded from the main query by default so you need to include the custom post type in the main query manually. This uses pre_get_posts which changes the main query before it is executed.