<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>archGFX &#187; query_posts</title>
	<atom:link href="http://archgfx.net/tag/query_posts/feed" rel="self" type="application/rss+xml" />
	<link>http://archgfx.net</link>
	<description>Austin web designer - Adam Freetly</description>
	<lastBuildDate>Thu, 19 Apr 2012 18:01:04 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Adding Search results to WordPress 404 pages</title>
		<link>http://archgfx.net/blog/2007/geek/blogging/adding-search-results-to-wordpress-404-pages</link>
		<comments>http://archgfx.net/blog/2007/geek/blogging/adding-search-results-to-wordpress-404-pages#comments</comments>
		<pubDate>Wed, 12 Sep 2007 16:56:26 +0000</pubDate>
		<dc:creator>adam</dc:creator>
				<category><![CDATA[blogging]]></category>
		<category><![CDATA[404]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[query_posts]]></category>
		<category><![CDATA[search]]></category>
		<category><![CDATA[usability]]></category>
		<category><![CDATA[wordpress]]></category>

		<guid isPermaLink="false">http://archgfx.net/blog/2007/geek/blogging/adding-search-results-to-wordpress-404-pages</guid>
		<description><![CDATA[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 [...]]]></description>
			<content:encoded><![CDATA[<p>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 <a href="http://sndbx.org">Sandbox</a>.  Looking at the page was especially frustrating, since I knew that the <acronym title='Uniform Resource Locator'><span class='caps'>URL</span></acronym> I was looking at was close to the name of the page I was looking for.  Inspired by <a href="http://www.smashingmagazine.com/2007/07/25/wanted-your-404-error-pages/">Smashing Magazine's 404 usability post</a>, I set off to figure it out.</p>
<p>I started with the <a href="http://weblogtoolscollection.com/archives/2004/08/05/404-search-function-for-wordpress/">404 Search from Weblog Tools Collection</a>, which simply redirects 404 pages to search results.  That's not technically correct behaviour, so I borrowed the first few lines of my <code>404.php</code> from their example:</p>
<p><code>
<pre class="brush: php; title: ; notranslate">
&lt;?php
$search_term = substr($_SERVER['REQUEST_URI'],1);
$search_term = urldecode(stripslashes($search_term));
$find = array (&quot;'.html'&quot;, &quot;'.+/'&quot;, &quot;'[-/_]'&quot;) ;
$replace = &quot; &quot; ;
$search_term = trim(preg_replace ( $find , $replace , $search_term ));
$search_term_q = preg_replace('/ /', '%20', $search_term);
get_header() ?&gt;
</pre>
<p></code></p>
<p>Then the rest of the 404 template, which you can  <a href="http://archgfx.net/blog/index.php/2006/history/1011001">check out here</a>, follows normally from <a href="http://sandbox-theme.googlecode.com/svn/trunk/404.php">the sandbox file</a>, until after the search field (which is important for usability).   I don't use this in my index or single page templates, because <a href="http://wpbits.wordpress.com/2007/08/21/making-wordpress-themes-ii-the-loop/#comment-618">404's shouldn't land there</a> (if they do, it's a problem with wp_query() ).  After the search field, we jump into a custom loop:</p>
<p><span id="more-1200"></span></p>
<p><code>
<pre class="brush: php; title: ; notranslate">
&lt;?php /* run the url as a query */
query_posts('s='. $search_term_q );
?&gt;
&lt;?php if ( have_posts() ) :
/* check to see if there are posts, before telling people something that isn't true */ ?&gt;
&lt;div class=&quot;content-column left&quot;&gt;&lt;h3&gt;You might have been looking for these posts&lt;/h3&gt;
&lt;ul&gt;
&lt;?php /* start the loop */
while ( have_posts() ) : the_post(); ?&gt;
&lt;li&gt;&lt;strong&gt;&lt;a href=&quot;&lt;?php the_permalink() ?&gt;&quot; title=&quot;&lt;?php printf(__('Permalink to %s', 'sandbox'), wp_specialchars(get_the_title(), 1)) ?&gt;&quot; rel=&quot;bookmark&quot;&gt;&lt;?php the_title() ?&gt;&lt;/a&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;abbr title=&quot;&lt;?php the_time('Y-m-d\TH:i:sO'); ?&gt;&quot;&gt;&lt;?php unset($previousday); printf(__('%1$s', 'sandbox'), the_date('', '', '', false), get_the_time()) ?&gt;&lt;/abbr&gt;&lt;br /&gt;
&lt;em&gt;
&lt;?php printf(__('Posted in %s', 'sandbox'), get_the_category_list(', ')) ?&gt;
. &amp;nbsp;
&lt;?php comments_popup_link(__('Comments (0)', 'sandbox'), __('Comments (1)', 'sandbox'), __('Comments (%)', 'sandbox')) ?&gt;
&lt;/em&gt;
&lt;/li&gt;
&lt;?php /* end the loop */
endwhile; ?&gt;
&lt;/ul&gt;&lt;/div&gt;
&lt;?php /* close the list before closing the ifelse */
else : endif; ?&gt;
</pre>
<p></code></p>
<p>You can read the comments, but basically, we take the <acronym title='Uniform Resource Locator'><span class='caps'>URL</span></acronym> and use <code>query_posts() </code>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 <acronym title='Uniform Resource Locator'><span class='caps'>URL</span></acronym>'s. and BTW, the fancy code highlighting in this post is courtesy of a <a href="http://wordpress.org/extend/plugins/syntaxhighlighter/">fancy new plugin</a>.</p>
<p><ins datetime="2007-09-12T22:36:46+00:00"><strong>UPDATE</strong>: Some additional code was used from <a href="http://www.arunrocks.com/blog/archives/2006/03/01/a-nearly-perfect-404-error-page-in-wordpress/">Arun's nearly perfect 404</a>.</ins></p>
]]></content:encoded>
			<wfw:commentRss>http://archgfx.net/blog/2007/geek/blogging/adding-search-results-to-wordpress-404-pages/feed</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
	</channel>
</rss>

