Adding custom post types to archive.php

At the pre_get_posts action, check if the query object is_main_query to target only the main archive query and exclude additional queries.

add_action('pre_get_posts', 'query_post_type');
function query_post_type($query) {
  if($query->is_main_query()
    && ( is_category() || is_tag() )) {
        $query->set( 'post_type', array('post','cpt') );
  }
}

Leave a Comment

tech