Make WordPress search more useful
March 23, 2009So, we all know it. Most times, the WordPress default search stuff kinda sucks. Someone pointed out to me that 2.6 or 2.7 started searching Page content as well as post content, but in times past it wasn’t like that – it would only search the post content.
However, this is a little post on how to make the search stuff a little more useful and less annoying.
First off, I’ll start with this article that was passed to me the other day: Make WordPress’ Search Function Suck Less. Yoast is pretty knowledgeable with WP stuff, and this is a pretty good article. So I’ll let this speak for itself.
What I am going to do is show you how the search results can be a little bit better – namely the 404 Page.
Honestly, this is something I wish I knew where I got the code from. Yet again, I didn’t note the source of this code, so I’m going to start off by telling you that I did not come up with this on my own – I found it somewhere, a long, long time ago in a galaxy far, far away. So if anyone happens to know where I did get this code from, please feel free to let me know and I’ll credit the true genius who came up with this. Actually, it came from Adam over at archgfx.
So what we’re going to do is offer up suggestions. Instead of the crappy “404 Page Not Found – would you like to try a search?” message that comes up by default, what we’re going to do is have WordPress search for similar search terms, and offer up alternatives if no exact matches are found. Say someone misspelled the term, or something, right? it would be cool if something more helpful came up.
So here we go. First, you want to open up your functions.php file (if you don’t have one, just make one) and insert the following code:
function search404() {
/* run the url as a query */
query_posts('s='. $search_term_q );
if ( have_posts() ) :
/* check to see if there are posts, before telling people something that isn't true */ ?>
<h5>Maybe you were looking for one of these?</h5>
<div id="results">
<?php $n = 1;
while ( have_posts() ) : the_post(); ?>
<h3>. <a href="<?php the_permalink() ?>" title="<?php the_permalink() ?>" rel="bookmark"><?php the_title() ?></a></h3>
<p class="date"><?php the_time('F j, Y') ?></p>
<div class="entry">
<?php the_excerpt(); ?>
</div>
<?php $n++; endwhile; ?>
<p><strong>If none of these work for you, you might try a different search term.</strong></p>
</div>
<?php /* close the list before closing the if/else */
else : endif;
}
Note that the error messages can, of course, be edited to reflect whatever you like. But basically the above function takes the URL and runs a query on it.
Now you need to open up your 404.php file (again, if you don’t have one, create one), and insert the following before the “get_header();” call:
$search_term = substr($_SERVER['REQUEST_URI'],1);
$search_term = urldecode(stripslashes($search_term));
$find = array ("'.html'", "'.+/'", "'[-/_]'") ;
$replace = " " ;
$search_term = trim(preg_replace ( $find , $replace , $search_term ));
$search_term_q = preg_replace('/ /', '%20', $search_term);
and then you want to replace all the regular 404 stuff (that comes default) with your call to the function:
<div class="post">
<h2>404 - Page Not Found</h2>
<div class="entry">
<p>Sorry, but that page cannot be found.</p>
<?php search404(); ?>
<!--/entry-->
</div>
<!--/post-->
</div>
And there you have it – a nice 404 Page that’s actually helpful
For the record, I thought I’d add a couple of other helpful 404 links that may assist you – I haven’t tried any these out, but they look mighty tasty:
Improve your WordPress Related Posts for 404’s
Nearly Perfect 404 Error Page in WordPress
Enjoy your 404-love!










P.O. Box 46
Pity your website’s cufon script doesn’t work properly in Internet Explorer. For a web design company, this is very bad news!
Well, while I appreciate you mentioning this to me, and the fact that you also emailed me on the issue – I really wish you would have left me actual contact information. In the comment AND in the email, you didn’t say what version (or what computer platform) you were using to view the site in. So I’m going to have to assume some things.
In response to this (and yes, I did approve the comments because I want people to publicly know what I have to say about it):
#1 I am NOT using a plugin. I’m using straight cufon. if there *is* a WP plugin, that would be awesome to know about it.
#2 After I received both the comment and the email, I checked myself in IE, both 7 and 8, on both XP and Vista. I also have a fine network of web design colleagues I’m a part of, and I put them question to them, as well. There is absolutely no problem seeing the cufon in IE7 or 8 that we have found. So I can only assume you cannot see it because you have a particular browser setting you are not sharing.
#3 You also do not mention what you *do* see. Because you say you “can’t see cufon” I can only assume you see the alternative text which *does* appear if you have javascript shut off.
#4 If you’re viewing in standalone versions, you won’t see it, because standalones don’t render it properly (this includes – as much as I am aware of – BrowserCam as well). If you’re using a standalone version, then I assume you’re a web designer of some kind (because “normal” people don’t use standalone versions – the only people I know that do are designers who use them to check websites). If that is the case, I can only assume you’re a competing designer who was checking it and didn’t see anything, and wanted to try and make me look bad on my own site. I know I, as a professional, would tell another designer this about their site privately in order to assist them, and provide contact information so they could ask me questions if they needed to so they could fix it. You did neither – so I’m *hoping* that this option is not the case. That would be really bad form on your part.
#5 if you were viewing in IE6, I don’t support IE6 anymore. The browser is outdated and unsafe. That’s by design. I *can*, but I choose not to. So if it’s IE6. well…you should be updating your browser. This might be an interesting site for you to peruse in your spare time