So I have the out of the box wordpress 'Search' widget that I want to expand. I used the below filter to have the ability to search for my custom post types inside the widget:
add_filter( 'pre_get_posts', function($query) {
if ($query->is_search) {
$query->set('post_type', [
'post', 'profile' ,'recipe', 'dish'
]);
}
});
The problem that I discovered is that it overwrote ALL search items on the website, including the search box inside the admin panel 'Posts', where I was getting custom post type results although they didn't live there.
Does anyone know how to improve the search functionality on the search widget to include custom post types without overwriting ALL search boxes throughout the site?
$_GET
param indicating that it's "a widget" so you can check against it inpre_get_posts
. If you're just looking to stop it from running on admin you can check againstis_admin()
and return early.