@PieterGoosen has a good description on why your pre_get_posts
callback is giving you a problem.
Here’s an alternative workaround to restrict the native search widget to the post post type:
/**
* Restrict native search widgets to the 'post' post type
*/
add_filter( 'widget_title', function( $title, $instance, $id_base )
{
// Target the search base
if( 'search' === $id_base )
add_filter( 'get_search_form', 'wpse_post_type_restriction' );
return $title;
}, 10, 3 );
function wpse_post_type_restriction( $html )
{
// Only run once
remove_filter( current_filter(), __FUNCTION__ );
// Inject hidden post_type value
return str_replace(
'</form>',
'<input type="hidden" name="post_type" value="post" /></form>',
$html
);
}
where we use adjust the output of the get_search_form()
function but only for the search widgets.