How to Disable Search Queries in WordPress

By ThemesDNA1 min read

Share:

Disabling search queries in WordPress is not a common practice, as the search functionality is very useful for many websites. However, if you need to disable search queries for a specific reason, you can do so by adding the following code to your theme's functions.php file:

function themesdna_disable_search_queries( $query, $error = true ) {
    if ( is_search() && ! is_admin() ) {
        $query->is_search       = false;
        $query->query_vars['s'] = false;
        $query->query['s']      = false;
        if ( true === $error ) {
            $query->is_404 = true;
        }
    }
}
add_action( 'parse_query', 'themesdna_disable_search_queries', 15, 2 );
Premium WordPress themes Made for WordPress   /   Ready for your story

Ready to Build?

Your next great website starts here.

Build more websites with one flexible collection—and keep every theme for life.

  • 56+ themes
  • Lifetime updates
  • Unlimited websites
  • No renewals

The complete collection

$75

One payment. No renewals.

Get the Themes Bundle — $75Purchase terms