By default, wordpress do not search for blank. It redirects to the home page for blank search. To prevent this we can use pre_get_posts hook. We can use pre_get_posts action to manipulate the query variables for specific pages. We can modify $wp_query object before any results are returned. In this example, we are using pre_get_posts before $wp_query returning false value. Use the following code on functions file inside your theme.
<?php // to avoid redirection from search result displaying in home page
function make_blank_search ($query){
global $wp_query;
if (isset($_GET['s']) && $_GET['s']==''){ //if search parameter is blank, do not return false
$wp_query->set('s',' ');
$wp_query->is_search=true;
}
return $query;
}
add_action('pre_get_posts','make_blank_search');
?>
0 comments:
Post a Comment