I want to make my search bar on the website show custom post type (courses) beside the blog posts.
I tried these codes But it did not work. The only thing that worked was the WP Extended Search plugin, but I want to do it by code.
function tg_include_custom_post_types_in_search_results( $query ) {
if ( $query->is_main_query() && $query->is_search() && ! is_admin() ) {
$query->set( 'post_type', array( 'post', 'courses') );
}
}
add_action( 'pre_get_posts', 'tg_include_custom_post_types_in_search_results' );
and this
function include_cpt_search( $query ) {
if ( $query->is_search ) {
$query->set( 'post_type', array( 'post', 'courses', 'custom_post_type' ) );
}
return $query;
}
add_filter( 'pre_get_posts', 'include_cpt_search' );
Please advise. Thank you.
?search=
etc thenis_search()
will befalse