In WordPress you will see a list of all posts and pages on the site if you omit to add a search string and then click the submit button. The only reason that I can think of why WordPress does things this way is because the search functions are also used in the admin pages e.g. on All Pages and All Posts. On both these pages there are filters that include ‘All Categories’ and ‘All Dates’. For both these filters the search string will be empty but still WordPress has to return all the entries.
When searching in WordPress you get a list of all posts and pages that contain the search string you entered if there are results to show.
If you enter a search string that does not exist in any post or page WordPress will tell you about this.
Now we come to the so-called ‘problem’. If you omit to add any search string and hit the search submit button you will see a list of all the posts and pages in the site.
This behaviour might seem strange at first but as mentioned already there is a reason for this. It would make more sense if an empty search could rather show the ‘Nothing Found’ warning.
I have stumbled upon many strange and wonderful solutions to this ‘problem’, but all of them are code intensive and most probably do not work.
All themes have a template to show search results (normally search.php). This template uses a loop to display search results. This loop starts with the code:
This loop checks to see if there are any posts in the search result and then shows these posts. All you need to do here is to check if there are posts within the search results and if a search string exists (an empty search string is not a search string). You need to find the loop shown above and then change the first line of the loop to:
That’s it, that’s all you need do. If you know of an easier way of doing this same thing I would love a heads-up in the comments.
Thanks for visiting.