The following code works in preventing our "Projects" from appearing in the site search results, but it is causing unintended consequences for various filtering on the admin backend. Is there any way to contain the function to only the front end search?
function ss_search_filter( $query ) {
if ( $query->is_admin() ) :
return $query;
endif;
if ( $query->is_search() && $query->is_main_query() ) {
// set the post type to only post types you want returned.
$query->set( 'post_type', ['page', 'post', 'product'] );
}
return $query;
}
add_action( 'pre_get_posts', 'ss_search_filter' );
The $query->is_admin()
is likely incorrect. Try changing it to is_admin()
.
Additional note: pre_get_posts
is an action, not a filter, so returning variables is incorrect.