How to change 'Sorry, no posts matched your criteria.'

What do you do with your readers when your blog doesn't supply what they're looking for? If you're most WordPress themes, you give them the rather ugly message, 'Sorry, no posts matched your criteria.' If you want to change this to something more friendly or useful, here's how to do it.

Firstly, find the file you want to edit. For most people, this will be the one that generates search results, so go to Appearance > Editor, and click search.php. Find the code that looks something like this.
<?php endwhile; else: ?>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>

Whatever's between else and endif is what displays if no search results are found. The _e function is for translation, so you don't necessarily need to preserve it. You could change the messsage, and add a link to your homepage:
<?php endwhile; else: ?>
<p>Sorry, I can't find any posts that match what you're looking for. Try the <a href="<? bloginfo('url');?>">home page</a>. </p>
<?php endif; ?>

Or you might like to repeat the search query:
<?php endwhile; else: ?>
<p>Sorry, I can't find any posts that match <? echo $_GET['s']; ?>. Please try searching again. </p>
<?php endif; ?>

This will display "Sorry, I can't find any posts that match [whatever the search query was]. Please try searching again."

Or you might even like to add a new search box in directly below the "I can't find anything" message. If your theme has a searchform.php file, this is simple:

<?php endwhile; else: ?>
<p>Sorry, I can't find any posts that match <? echo $_GET['s']; ?>. Please try searching again. </p>
<? include("searchform.php"); ?>
<?php endif; ?>

'Sorry, no posts matched your criteria' also appears in other theme files, though readers are much less likely to see it on those pages. single.php, archives.php and even index.php might all carry the same message, so if you really dislike it, you'll need to edit them too.

Share this post:
  • email
  • del.icio.us
  • Facebook
  • FriendFeed
  • LinkedIn
  • StumbleUpon
  • Tumblr
  • Twitter

Tags: ,

Posted by Sue on June 13, 2009 in WordPress.

6 comments to "How to change 'Sorry, no posts matched your criteria.'"

  1. Ron wrote:

    How about a way to stop this message from happening when you do a preview of your post? I've been getting this error since version 2.7 (I'm on 2.8.4 now). The only way to preview a post is to publish it publically. That's no solution.

    • Sue wrote:

      Hi Ron,
      That's an interesting one. I would only expect for that to happen if you had signed out of WP between writing the post and trying to preview it: obviously, draft posts are only visible if you're signed in.

      So, possibilities:
      ~ most likely: are you using that feature where your blog address is not the same as the physical address on the server? If so, I think that may cause difficulties with previewing. Easiest fix: make sure WP address and blog address (under Settings > General) are the same, and point to a directory that's physically there on your server.
      ~ have you recently changed permalink format? If you used to be using ?p=xx and are now on pretty ones, I think you'll need to clear all your cookies/cache and start over (but if it's been going on since 2.7, it's probably not that).
      BUT if you type in
      http://rdesgr.com/WhatsAllThisThen/?p=xx
      where xx is the number of a published post, can you see that, or do you get the error message? That might help to narrow down where the problem is?

      Otherwise:
      ~ does your web host have Mod Rewrite enabled? If not, I *think* that will be why it doesn't work. Ask them to enable it.
      ~ you're not doing something odd like writing a post in one browser and trying to preview it in another, are you? If you are, you'll need to sign into admin on both for it to work.
      ~ do you have cookies blocked? (in which case, I'd expect you to be having more problems with WP than just this though)
      ~ if you *save* the post and *then* preview it, does that help at all?
      ~ if you use a different browser, do you get the same problem?

      • Ron wrote:

        Hello Sue,

        Thank you for all of your excellent suggestions. I have made sure the web address matched the blog address and have cleared the cache and cookies from my Firefox and IE browsers. I will do your other recommendations and report back.

        Thank you so very much for taking the time to write. I have your blog on my netvibe's RSS feed now.

        Ron

        • Sue wrote:

          What you've got entered for the URLs - they should both be just http://rdesgr.com/WhatsAllThisThen/ - if you've got a www in there, that may be the problem.

          Otherwise - check you have an .htaccess file and that it's writable. It's something where your blog isn't recognising that /p=123 and /2009/09/post-title-here are the same thing, I'm sure. And as I've never heard of this problem anywhere else, I'd like to solve it ;-)

  2. Sam Brown (@brownsam) wrote:

    Hi Sue,

    Really liking your site - really useful stuff for a semi-beginner. Is it possible to add a photo into the 404 screen? I've seen some great 404 pages and fancy adding in something a bit different to the norm.

    Thanks,

    Sam

    • Sue (@blogmum) wrote:

      Hi Sam,
      You can make the 404 page be whatever you want: just add a 404.php into your theme folder and you can have whatever images, text etc. you like. Making them a bit fun is great, BUT don't make them so fun you confuse the user more - you need to also say "the page you were looking for isn't here" and provide some ways to find whatever it was, or navigate further.

Leave a Reply