I would recommend not to perform the search right away but give the user the option to click a link which would activate the search.
– Based on the url slug requested (if permalink)
– Based on the keywords used to reach the page (if coming from google)
A place to start could be:
http://wasabi.pbwiki.com/Related%20Entries
Here is an example of my 404 page (Note my posts ends with .html, which I take benefit of when trying to find slug-terms.
<?php
function get_search_phrase($referrer_url, $d){
$terms = null;
$query_array = array();
$query_terms = null;
// Get raw query
$query = explode($d.'=', $referrer_url);
$query = explode('&', $query[1]);
$query = urldecode($query[0]);
// Remove quotes, split into words, and format for HTML display
$query = str_replace("'", '', $query);
$query = str_replace('"', '', $query);
$query_array = preg_split('/[\s,\+\.]+/',$query);
$query_terms = implode(' ', $query_array);
$terms = htmlspecialchars(urldecode(trim($query_terms)));
return $terms;
} // end get_search_phrase
get_header();
get_sidebar();
$query = '';
if ($_SERVER['HTTP_REFERER'])
{
$referrer_url = attribute_escape($_SERVER['HTTP_REFERER']);
$url = parse_url($referrer_url);
if (strstr($url['host'],'google'))
$query = get_search_phrase($referrer_url, 'q');
else
if (strstr($url['host'],'yahoo'))
$query = get_search_phrase($referrer_url, 'p');
else
if (strstr($url['host'],'live'))
$query = get_search_phrase($referrer_url, 'q');
$query = trim($query);
}
$slug_terms = attribute_escape($_SERVER['REQUEST_URI']);
$slug_terms = substr($slug_terms, strrpos($slug_terms, '/'), strlen($slug_terms));
$search = array ('@[\/]+@', '@(\..*)@', '@[\-]+@', '@[\_]+@', '@[\s]+@', '@archives@','@(\?.*)@','/\d/');
$replace = array (' ', '', ' ', ' ', ' ', '', '','');
$slug_terms = preg_replace($search, $replace, $slug_terms);
$slug_terms = trim(htmlspecialchars($slug_terms));
?>
<div class="page" id="mainpage">
<h4>404 - Page not found</h4>
The page you tried to access cannot be found. You may have tried to
use an outdated link or you may have typed the address (URL) incorrectly.<br/>
<br/>
There are several ways to find page you are looking for:
<ul>
<li>Reload/Refresh the page, as sometimes the 404 can be a fluke.</li>
<li>Check the address (URL) for correctness (Spelling errors etc.).</li>
<li>Use the search feature to find the wanted page.
<?php if (!empty($query) || !empty($slug_terms)) : ?>
<ul>
<?php if (!empty($query)) : ?>
<li><a href="<?php echo get_bloginfo('home'); echo '?s='; echo $query; ?>">Repeat search</a> using the original query (<b><?php echo $query; ?></b>).</li>
<?php endif; ?>
<?php if (!empty($slug_terms)) : ?>
<li><a href="<?php echo get_bloginfo('home'); echo '?s='; echo $slug_terms; ?>">Perform search</a> using url slug terms (<b><?php echo $slug_terms; ?></b>).</li>
<?php endif; ?>
</ul>
<?php endif; ?>
</li>
</ul>
</div>
<?php
get_footer();
?>