
This snippet disables the default WordPress search results page.
If someone visits a search URL like /?s=keyword, it will show the 404 page instead. If no 404 page is available, it redirects them to the homepage.
It’s useful for sites that don’t use search and want to prevent visitors from landing on unnecessary search result pages.
Copy & paste the scripts before the </body> tag of your project. If you added them before for another setup, skip this step.
Place the code in an HTML widget or add it through Elementor → Custom Code (before the closing </body> tag) either globally or only on selected pages.
Paste the code through the page or site settings, or add it via Elementor → Custom Code (before </body>) for broader use.
Paste the script through Elementor → Custom Code (set to load after </body>) for site-wide or page-specific loading.
Place the PHP snippet in your theme’s functions.php file or add it using a code snippets plugin to enable the logic.
add_action('template_redirect', function () {
if (is_search() && !is_admin()) {
global $wp_query;
$wp_query->set_404();
status_header(404);
nocache_headers();
$template = get_query_template('404');
if ($template) {
include $template;
} else {
wp_safe_redirect(home_url(), 302);
}
exit;
}
});Some solutions only work on the live site. Always publish and test after each change, as results may not appear in the editor.