WordPress – Category Page Not Showing Posts With Custom Post Type

By Forrest Smith - Drempd.com

I had created a post type for news articles. Unfortunately, when categorizing the pages, and trying to view the category from the front end, those posts weren't appearing.   Apparently that doesn't work for cusotm post types by default. To get them to show, add this to the functions.php file:

function namespace_add_custom_types( $query ) {
   if( (is_category() || is_tag()) && $query->is_archive() && empty( $query->query_vars['suppress_filters'] ) ) {
      $query->set( 'post_type', array(
       'post', 'news'
      ));
   }
}
add_action( 'pre_get_posts', 'namespace_add_custom_types' );