Wordpress | Add Custom post types to tags and categories archives

// Add Custom post types to tags and categories archives
function add_cpts_to_archive_page( $query ) {
  if( (is_category() || is_tag()) && $query->is_archive() && empty( $query->query_vars['suppress_filters'] ) ) {
    $query->set( 'post_type', array(
     'post', 'custom-post-type-1', 'custom-post-type-2'
        ));
    }
    return $query;
}
add_filter( 'pre_get_posts', 'add_cpts_to_archive_page' );

 

This website may contain fan art inspired by existing characters from films, TV shows, or games. I don't own any rights to those characters — any copyright owner wishing to have a fan art removed can contact me here. This is a personal portfolio and store. It uses only the essential cookies needed for the site, cart, and account to work — no analytics, no tracking, no advertising.