Register Advertise with usHelp Desk Today's Posts Search

Closed Thread
Thread Tools Search this Thread
Unread 18th Aug 2013, 02:20 PM   #1
DJL
Advanced Warrior
War Room Member
 
DJL's Avatar
 
Join Date: 2008
Location: Texas, USA.
Posts: 577
Thanks: 54
Thanked 248 Times in 164 Posts
Default
Annoying Behavior of WordPress Search Function
Share on: 
fb share twitter share gplus share more share

Re: WordPress 3.6
Twenty Thirteen Theme

This behavior may be different if you are using other WP versions or different themes.

I observed that, if you click the Search button without entering anything in the text input box, WordPress displays results containing the complete text of EVERY post you have published, and does not label the output as "Search Results for...". If you enter one or more spaces in the input box (and nothing else), it returns a slightly different result, containing excerpts of all published posts and pages!

To me, this seems undesirable, so I did some research on the issue.

I found numerous discussions and proposed ways of modifying this behavior, but none of them were quite satisfactory. Some of them were so convoluted that they gave me a headache!

With credit to WebDevSecrets.com, I found what I believe is the simplest solution. I modified the recommended code slightly, and would like to share it here with you:

Add the following code to your theme's functions.php file:

Code:
function allowBlankSearch($query)
{
    global $wp_query;
    // If the search is empty, return true
    if(isset($_GET['s']) && (trim($_GET['s']) == ''))
    {
        $wp_query->set('s', 'nothing-nada-zilch');
        $wp_query->is_search = true;
    }
    return $query;
}
 
add_action('pre_get_posts', 'allowBlankSearch');
With this code installed, a blank search will now return a page saying "Nothing found," which I believe makes the result more meaningful to the user.

If you are using a child theme, in which you may not have a functions.php file, create a new text file with that name, insert the above code, and enclose it between the following two lines:
Code:
<?php

?>

None are more hopelessly enslaved than those who falsely believe they are free.
--Johann Wolfgang von Goethe, Elective Affinities (1809)
DJL is offline  
Closed Thread


Bookmarks

Tags
annoying, behavior, search, wordpress


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




All times are GMT -6. The time now is 09:27 PM.