Custom post types are by default excluded from the main query except on taxonomy pages and custom post type archives.
You can simply use
pre_get_posts to correctly alter the main query (alter the query variables before the SQL query is built and executed) to your needs.
Just a few notes on
pre_get_postsruns front end and back end queries, so it is very important to do the
is_admin()check for queries only meant to run front end or back end
pre_get_postsalters all custom instances of
WP_Query) and the main query (which also uses
WP_Query). You would want to use the
is_main_query()check to specifically only alter the main query.
You can do the following in a plugin or your theme’s functions.php (or create a Child Theme and put it in that functions.php):
add_action( 'pre_get_posts', function ( $q )
if ( !is_admin() // Only target front end queries
&& $q->is_main_query() // Only target the main query
&& $q->is_category() // Only target category archives [comment out if not needed]
&& $q->is_tag() // Only target tag archives [comment out if not needed]
$q->set( 'post_type', ['post', 'custom_post_type'] ); // Change 'custom_post_type' to YOUR Custom Post Type
// You can add multiple CPT's separated by comma's
- Target WordPress Category archives with
- Target WordPress Tags archives with
- You can target other specific queries (like search, 404, etc.) by using those specific
is_functions from WP
- Target the specific post type you’re wanting to add by including it in the
$q->setabove. Make sure you do not remove
'post'as this will prevent posts from showing.
From WordPress Stack Exchange: pre_get_posts for Custom Post Types in Categories & Tags