Adding Search results to Wordpress 404 pages

Okay, so all this redirection business, and the 404 logs it generates, made me really get annoyed with seeing the blank 404 page I have. It's just the basic page that comes with Sandbox. Looking at the page was especially frustrating, since I knew that the URL I was looking at was close to the name of the page I was looking for. Inspired by Smashing Magazine's 404 usability post, I set off to figure it out.

I started with the 404 Search from Weblog Tools Collection, which simply redirects 404 pages to search results. That's not technically correct behaviour, so I borrowed the first few lines of my 404.php from their example:


<?php
$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);
get_header() ?>

Then the rest of the 404 template, which you can check out here, follows normally from the sandbox file, until after the search field (which is important for usability). I don't use this in my index or single page templates, because 404's shouldn't land there (if they do, it's a problem with wp_query() ). After the search field, we jump into a custom loop:


<?php /* run the url as a query */
query_posts('s='. $search_term_q );
?>
<?php if ( have_posts() ) :
/* check to see if there are posts, before telling people something that isn't true */ ?>
<div class="content-column left"><h3>You might have been looking for these posts</h3>
<ul>
<?php /* start the loop */
while ( have_posts() ) : the_post(); ?>
<li><strong><a href="<?php the_permalink() ?>" title="<?php printf(__('Permalink to %s', 'sandbox'), wp_specialchars(get_the_title(), 1)) ?>" rel="bookmark"><?php the_title() ?></a></strong><br />
<abbr title="<?php the_time('Y-m-d\TH:i:sO'); ?>"><?php unset($previousday); printf(__('%1$s', 'sandbox'), the_date('', '', '', false), get_the_time()) ?></abbr><br />
<em>
<?php printf(__('Posted in %s', 'sandbox'), get_the_category_list(', ')) ?>
. &nbsp;
<?php comments_popup_link(__('Comments (0)', 'sandbox'), __('Comments (1)', 'sandbox'), __('Comments (%)', 'sandbox')) ?>
</em>
</li>
<?php /* end the loop */
endwhile; ?>
</ul></div>
<?php /* close the list before closing the ifelse */
else : endif; ?>

You can read the comments, but basically, we take the URL and use query_posts() to run it as a search, and return the results as a list, if there are any. So far, it works for posts that I've referenced elsewhere, but it's not returning the actual post itself. That's probably because I use category slugs in my URL's. and BTW, the fancy code highlighting in this post is courtesy of a fancy new plugin.

UPDATE: Some additional code was used from Arun's nearly perfect 404.

5 Comments

  1. Posted 12 Sep 2007 at 2:37 |
    Permalink Quote

    This is incredibly clever, Adam. I’m definitely going to use this. It’s an excellent point: is a 404 page any better than just search results with a 404 message? Probably not.

  2. Posted 12 Sep 2007 at 4:29 |
    Permalink Quote

    as far as using this yourself, do beware. i’m pretty sure there’s supposed to be some preg_replace() happening so that it returns results for the terms in the address, not the address itself. i’ll update the post when i get my head around it.

  3. Posted 12 Sep 2007 at 5:41 |
    Permalink Quote

    yep. Digging through the comments on the WLTC post I found that Arun already figured this all out. the appropriate PHP magics have been added to the post.

  4. Posted 12 Sep 2007 at 6:18 |
    Permalink Quote

    ETA: i fixed the .htaccess problem that was making me see so many 404’s. thusly, the link in the post now takes you to the post, instead of a 404. you can use this link instead: http://archgfx.net/blog/wank

  5. Posted 7 Dec 2007 at 5:14 |
    Permalink Quote

    Excellent! This is just what I was looking for :) I’ve implemented in my site (http://www.seotier.com/404page). Thank you very much :D

2 Trackbacks/Pingbacks

  1. archGFX | Dream in Infrared 0.6 for WordPress 2.3

    [...] in Infrared 0.6 is done, which adds compatibility with WordPress 2.3’s native tagging.  It also adds search results to 404 pages, as well as a list of recent posts.  There’s also a bug fix, so that the Table of Contents [...]

  2. En bedre tegneserieside, del 1: 404 not found at superevil.org

    [...] syntes at jeg var færdig, da jeg faldt over denne artikel - søgeresultater inkluderet på 404, gættet ud fra den URL man har forsøgt at tilgå? Nice. [...]

Post a Comment

Your email is never published nor shared. Comments that do not follow the guidelines may be removed. By submitting a comment here you grant this site a perpetual license to reproduce your words and name/web site in attribution. Required fields are marked *
↓ Preview