Skip to content

Commit

Permalink
Return no result for empty search queries
Browse files Browse the repository at this point in the history
  • Loading branch information
7studio committed Jan 3, 2017
1 parent 9a02e9d commit 798e148
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions includes/search.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,4 +138,25 @@ function thistle_highlight_search_terms( $text ) {
add_filter( 'the_excerpt', 'thistle_highlight_search_terms', 999 );
add_filter( 'the_content', 'thistle_highlight_search_terms', 999 );

if ( ! function_exists( 'thistle_handle_empty_search_query' ) ) {
/**
* Short-circuits the search query for that returns no result
* if there is not searched term.
*
* By default, WordPress returns all results if there is not searched term.
* This behaviour has no sense.
*
* @param string $where The WHERE clause of the query.
* @param WP_Query $wp_query The WP_Query instance (passed by reference).
* @return string
*/
function thistle_handle_empty_search_query( $where, $wp_query ) {
if ( $wp_query->is_main_query() && $wp_query->is_search() && empty( $wp_query->query_vars['s'] ) ) {
$where = ' AND 0=1';
}

return $where;
}
}
add_filter( 'posts_where_request', 'thistle_handle_empty_search_query', 10, 2 );
}

0 comments on commit 798e148

Please sign in to comment.