I found out just recently that in some cases when you perform a blank search on a WordPress site you are redirected to the front page with an error message. If you encounter this issue simply apply the fix below, which modifies the blank search into a search for one space. This is enough to resolve the error page and land you on the search page.
Add the following code to your theme’s functions.php
file to fix this annoying behavior:
if(!is_admin()){
add_action('init', 'search_query_fix');
function search_query_fix(){
if(isset($_GET['s']) && $_GET['s']==''){
$_GET['s']=' ';
}
}
}
vinhboy says
thanks for the hint. this worked for me.
Ronald Joseph says
Thanks Your Code Work For ME
sathish says
Thnaks it’s awesome
Baruch Youssin says
I want to thank you for your 2011 page on Fixing the empty searches.
It still works fine and your solution is much simpler than everything else I have seen on the net while the problem got worse as the empty search on my WP site does not return the list of all pages with their initial lines (according to the WP logic) but rather the FULL text of all posts and pages including all the long ones! (Apparently, it is a botched front page; I am using a static front page.)
Thanks!