Hi, I reviewed and integrated the code in these 2 posts:
https://docs.pluginize.com/article/23-post-type-posts-in-search-results
https://docs.pluginize.com/article/17-post-types-in-category-tag-archives
But I have not been successful in getting the CPTs to appear in searches or categories.
This is the code I added to the functions.php file following the instructions provided:
// To display CPTUI posts in archives
function my_cptui_add_post_types_to_archives( $query ) {
// We do not want unintended consequences.
if ( is_admin() || ! $query->is_main_query() ) {
return;
}
if ( is_category() && empty( $query->query_vars['suppress_filters'] ) ) {
// Replace these slugs with the post types you want to include.
$cptui_post_types = array( '2018_artists' );
$query->set(
'post_type',
array_merge(
array( 'post' ),
$cptui_post_types
)
);
}
}
add_filter( 'pre_get_posts', 'my_cptui_add_post_types_to_archives' );
// To display CPTUI posts in search
function my_cptui_add_post_type_to_search( $query ) {
if ( $query->is_search() ) {
// Replace these slugs with the post types you want to include.
$cptui_post_types = array( '2018_artists' );
$query->set(
'post_type',
array_merge(
array( 'post' ),
$cptui_post_types
)
);
}
}
add_filter( 'pre_get_posts', 'my_cptui_add_post_type_to_search' );
Searches and categories return: SORRY, NO POSTS FOUND
Any idea what the problem might be?