Doc WordPress WordPress Tricks, Tips & Snippets

23Jan/170

Allow empty search

The default WordPress search function just redirects you to the home page should the search form be empty, which can be confusing for the visitors. Should you prefer to correctly display the search result page with a "Sorry your search returned no results instead" message you can fix this by adding this code to your functions.php:

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

You can test this behaviour right here on docwordpress.com.

Comments (0) Trackbacks (0)

No comments yet.


Leave a comment

No trackbacks yet.