Empty search shows home page on wordpress

5 years ago by in code, snippets Tagged:

OK, so you have a site. It’s not set up like a blog, so you don’t have a blog or posts page.

You notice that submitting the search form with nothing in it sends you to a broken home page.

There are all sorts of madnesses touted to fix this involving modifying the core wordpress code, which is a terrible idea given how often it updates.

stick this in your functions and ¬°presto pronto! all better. This will just send you to your search page and show everything with a space in it..

add_filter( 'request', 'my_request_filter' );
function my_request_filter( $query_vars ) {
if( isset( $_GET['s'] ) && empty( $_GET['s'] ) ) {
$query_vars['s'] = " ";
}
return $query_vars;
}

‚Äč

Eclecticism is the light and the way. Parsnips are fantastic. Celeriac is evil.