Candy, A Journal by a James

Improve your WordPress: related posts for 404’s

Second in a series of art­icles about tinker­ing with improv­ing your WordPress install­a­tion, we return to cus­tom 404 error pages; adding a list of pos­sibly related posts when vis­it­ors have fol­lowed an out­dated link. Other 404 error page improve­ments can be found in the first art­icle of this series.

One of the most use­ful things on a 404 page is a dir­ect link to the page vis­it­ors were try­ing to get to. Now we can’t read minds, but we do know the URI (explained in the third para­graph of the pre­vi­ous art­icle) and that’s good enough. The fol­low­ing code is adap­ted from this archGFX art­icle. The method used to trans­form the URI into a search query is very simple. If you would like a more advanced please refer to “A bet­ter 404 — Redux” at Urban Mainframe, where Jonathan Hollin expounds on his (down­load­able!) 404 page code.

There are two parts to this “related posts” code. The first part makes it pos­sible to get from “/wrong/link.html” (the URI) to “wrong link” (the search query).

Here it is:

<?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);

?>

That’s all there is to it. If you, how­ever, don’t want to make “/wrong/link.html” into “wrong link”, but rather just “link” (because the first part of the URI is likely to be use­less), use the fol­low­ing on the fourth line:

$find = array ("'.html'", "'.+/'", "'[-/_]'") ;

The second part con­sists of telling WordPress to use the search terms and out­put­ting a list of art­icles based on the results.

In this case the loop will out­put a linked title, a line-break and then the date of the art­icle. You can edit out the line-break and the date if you want. Also, you can change OL to UL if you want an unordered list out­put­ted.

<?php

query_posts(‘s=’. $search_term_q );

if ( have_posts() ) :

?>

<h1>Possibly related content</h1>

<ol>

<?php while ( have_posts() ) : the_post(); ?>

<li><a href=”<?php the_permalink() ?>”><?php the_title() ?></a>

<br>

<?php unset($previousday); printf(__(‘%1$s’, ‘sand­box’), the_date(”, ”, ”, false), get_the_time()) ?>

</li>

<?php  end­while; ?>

</ol>

<?php else : endif; ?>

That’s all there is to it! Once you’ve added this code your 404 error page should look a little some­thing like this.

To con­clude, add the fol­low­ing to your 404.php, wherever you think it fits best, to add :

<?php /* Transforming the URI into search terms */

$search_term = substr($_SERVER[’REQUEST_URI’],1);

$search_term = urldecode(stripslashes($search_term));

$find = array (“’.html’”, “‘[-/_]’”) ;

/* If you only want the last term of the URI, use the fol­low­ing instead:

$find = array (“’.html’”, “‘.+/’”, “‘[-/_]’”) ;  */

$replace = ” ” ;

$search_term = trim(preg_replace ( $find , $replace , $search_term ));

$search_term_q = preg_replace(‘/ /’, ‘%20′, $search_term);

?>

<?php /* Use the search terms to run a query */

query_posts(‘s=’. $search_term_q );

/* check to see if there are posts */

if ( have_posts() ) :

?>

<h1>Possibly related content</h1>

<ol>

<?php /* start the loop */  while ( have_posts() ) : the_post(); ?>

<li><a href=”<?php the_permalink() ?>”><?php the_title() ?></a>

<br>

<?php unset($previousday); printf(__(‘%1$s’, ‘sand­box’), the_date(”, ”, ”, false), get_the_time()) ?>

</li>

<?php /* end the loop */  end­while; ?>

</ol>

<?php else : endif; ?>

If you’ve any ques­tions, don’t hes­it­ate to ask!

3 Responses to “Improve your WordPress: related posts for 404’s”

  1. What did you intend this to do?

  2. Well, that didn’t work.

    What did you intend this to do?

    < ?php unset($previousday); printf(__(’%1$s’, ’sand­box’), the_date(”, ”, ”, false), get_the_time()) ?>

    • That code adds the time of pub­lic­a­tion under­neath (or behind, depend­ing on how you style it) the post title. That gives people a sense of how old it is, help­ing them to see if it’s relevant.

      Doesn’t it work for you?