Use the following snippet to restrict the default search form displayed on 404 and search results pages to specific post types.
In this example, searches are limited to regular posts and pages. You can replace post and page with any registered post types that you want to include.
add_filter('search_form_args', function ($args) {
if (
(is_404() || is_search())
&& ! isset($args['ct_post_type'])
) {
$args['ct_post_type'] = ['post', 'page'];
}
return $args;
});
The snippet only changes forms that do not already define a ct_post_type value. Search forms that explicitly specify their own post types remain unchanged.