Disabling WordPress search can be a practical decision for certain websites, helping streamline the user experience and potentially improve performance. By following these steps to modify your theme’s functions.php file, you can easily disable the search functionality without the need for additional plugins. Just remember to create a child theme to ensure that your changes remain intact through theme updates, and always make backups before making any modifications to your WordPress files.
/**
* Disable search query.
*
* @param WP_Query $query The WP_Query object representing the current query.
* @param bool $error Whether to force a 404 error response for the query (default is true).
* @return void
*/
function disable_search_query( $query, $error = true ) {
if ( is_search() ) {
$query->is_search = false;
$query->query_vars['s'] = false;
$query->query['s'] = false;
if ( $error ) {
$query->is_404 = true;
}
}
}
add_action( 'parse_query', 'disable_search_query' );