This morning I was trying to write up a custom search results template for WordPress using Twitter’s Bootstrap and run into a strange situation. First, here’s what my custom form’s HTML looked like:

Everything seemed to be working just fine, until I started writing up the code for no search results found. When clicking the submit button without typing any search text, pages and posts would still show up in the results!

After some Googling, I found out that the WordPress team has made this intentional. Their perspective is that some results should at least show up in this case. WordPress hits the home page using HTTP GET and passes your search query to an “s” parameter:

So apparently, when WordPress sees an empty search string it doesn’t even use the “is_search” page handler and that’s why this happens (it actually uses the “is_home” handler instead).

Since my client wanted to simply display no search results in this situation, here’s what I did. First I went to the bottom of my “functions.php” file found in my theme directory and added the following (you can create the file if there isn’t one there already):

This bypassed the standard WordPress behavior and allowed me to send the request along to my theme’s search template (“search.php” file found again under my theme).

Then it was a matter of handling the empty GET parameter in my “search.php” template. Here’s my complete template, keeping in mind that I am using Twitter Bootstrap to style my output:

My template includes some other functions as well, such as custom footer paging and my standard header and footer for the site, which are not important in this example.

13 Responses to How to handle and customize an empty search query in WordPress

  • Ariana

    its better

  • Jess

    Thanks for your post! This helped me solve my issue of formatting results when an empty search string is entered to the WordPress search bar.

  • Fey

    Thanks!  This was driving me nuts.  One little addition to my child theme’s functions and everything else fell into place.

    This should really be default behavior…

    • Evagoras Charalambous


      I will agree with you there – it should be there default behavior. This one stumped me for a while as well.

  • Pankaj

    Hello There,

    You truly rock!

    I almost wasted too much of time in this, and searched everywhere but found nothing!

    Luckily got your solution and it worked like charm.

    Thanks a ton for putting this up for everyone, May god bless you.

  • Skip

    Slightly at a tangent do you think it would be good practice to add content to an empty search results page. For example direct someone to a an index page or maybe include a recommended posts social plugin? I’m thinking about this and wonder if redirecting an empty search to a new template would be a good solution. Wondered what you think?

    • Evagoras Charalambous

      First, sorry for the delay. 🙂
      Traditionally in search results pages it was considered good practice to display a sitemap if no results were found, perhaps a one or two level deep link structure of your site’s contents. Anything else you add to a no-results-page should really be up to you. It’s also quite possible that the user enters a search query but nothing is found, which then brings us back to the same issue of what to show to the user.
      In my opinion a separate template/page or a search results page with something displaying to help the user is a better way of doing things, rather than simply showing no results. The same could be said for a 404 page-not-found of course, which could again work in a similar fashion.
      You could display a list of recommended posts like you mention or use widgets to insert things like the latest comments, latest forum posts, most read posts, etc. the sky is the limit really and it just depends on how much time you are willing to spend on this.

  • Internet

    Thanks a lot, it works perfectly.

  • Owen

    You’re missing a closing PHP tag on line 23

    • Evagoras Charalambous

      I don’t see it! Line 23 is this:
      </div><!--/.span3 -->

      What am I missing?

      • Nizam Kazi

        Bootstrap 3.0 changed span into col, check converting from old to new bootstrap guidelines.

  • Akro Web

    Thank you very much for this post.

    Sorry for my English, I’m French. I searched long on French sites to find a solution to customize a request empty search in WordPress, without success.

    thank you

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.