<?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>Webstractions Web Development &#187; code</title>
	<atom:link href="http://webstractions.com/tag/code/feed/" rel="self" type="application/rss+xml" />
	<link>http://webstractions.com</link>
	<description>Abstract thoughts in web design and development.</description>
	<lastBuildDate>Wed, 02 Mar 2011 12:22:28 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1</generator>
		<item>
		<title>Undocumented WordPress Query Function: wp_reset_query()</title>
		<link>http://webstractions.com/wordpress/undocumented-wordpress-query-function-wp_reset_query/</link>
		<comments>http://webstractions.com/wordpress/undocumented-wordpress-query-function-wp_reset_query/#comments</comments>
		<pubDate>Thu, 26 Feb 2009 18:07:12 +0000</pubDate>
		<dc:creator>Ronnie T. Dodger</dc:creator>
				<category><![CDATA[Wordpress]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[is_front_page()]]></category>
		<category><![CDATA[is_home]]></category>
		<category><![CDATA[query_posts()]]></category>
		<category><![CDATA[recent entries]]></category>
		<category><![CDATA[sidebar]]></category>
		<category><![CDATA[widgets]]></category>
		<category><![CDATA[wordpress functions]]></category>
		<category><![CDATA[wordpress theme]]></category>
		<category><![CDATA[wp_reset_query()]]></category>

		<guid isPermaLink="false">http://webstractions.com/?p=23</guid>
		<description><![CDATA[Creating a conditional sidebar widget in Wordpress turned into an odyssey of discovery. How an obscure usage of query_posts will bork your is_home() conditional tags.]]></description>
			<content:encoded><![CDATA[<p>Working with WordPress and coding themes is fairly easy to me. However, as is with every project, there comes a time in the process where you eventually pull your hair out. Such was the case when developing some simple<em> if logic</em> for the sidebar.</p>
<p>My goal was simple &mdash; suppress the Blogroll and Meta sections in the sidebar for any page except the Home Page. Logically, I thought, just surround the sections with the conditional tag is_home() should accomplish this simple feat. The WordPress Codex even has a <a href="http://codex.wordpress.org/Conditional_Tags#Variable_Sidebar_Content">dynamic sidebar example</a> for the use, pretty straight forward. </p>
<p>Take the following snippet of sidebar code for example. I am displaying a list of recent posts, immediately followed by the conditional blogroll.</p>
<pre name="code" class="html">
<ul class="sidebar_list">
<li class="widget">
<h3>Latest Blog Entries</h3>
<ul>
      &lt;?php query_posts('showposts=10'); ?&gt;
      &lt;?php if (have_posts()) : while (have_posts()) : the_post(); ?&gt;
<li>&lt;a href="&lt;?php the_permalink() ?&gt;"&gt;&lt;?php the_title() ?&gt;&lt;/a&gt;</li>

      &lt;?php endwhile; endif; ?&gt;
      </ul>
</li>

   &lt;?php if(is_home() &#038;&#038; !is_paged()): ?&gt;
<li class="widget">
<h3>Blogroll</h3>
<ul>
      &lt;?php  wp_list_bookmarks('title_li=&#038;categorize=0'); ?&gt;
      </ul>
</li>

   &lt;?php endif; ?&gt;
</ul>
</pre>
<p>The above code returns true for <kbd>is_home()</kbd> on each and every non-home page or post. Why is that? This is where I lost quite a bit of hair, mostly from the left side of my head and therefore most of the insulation to the thinking part of the brain. Good thing the heat works around here.</p>
<p>It took quite a lot of searching. This is not an easy thing to Google for, but eventually came across some answers.</p>
<p>The culprit in the code was the use of <kbd>query_posts()</kbd> in the recent posts widget. If I swapped the positions of the two widgets, <kbd>is_home()</kbd> functioned correctly. The query_posts() function has an obscure little bug if you do not destroy it properly with a call to <kbd>wp_reset_query()</kbd>.</p>
<p>There isn&#8217;t any documentation on <kbd>wp_reset_query()</kbd> in the Codex, so I will cite the comment heading from the WordPress core file, query.php, which this function is from:</p>
<p><cite>wp_reset_query() destroys the previous query and setup a new query. This should be used after  query_posts() and before another query_posts(). This will remove obscure bugs that occur when the previous wp_query object is not destroyed properly before another is setup.</cite></p>
<p>So I added that function immediately following the endwhile in the recent comments widget, and bam, everything was right in the universe again.</p>
<pre name="code" class="html">
      ....
      &lt;?php query_posts('showposts=10'); ?&gt;
      &lt;?php if (have_posts()) : while (have_posts()) : the_post(); ?&gt;
         &lt;li&gt;&lt;a href="&lt;?php the_permalink() ?&gt;"&gt;&lt;?php the_title() ?&gt;&lt;/a&gt;&lt;/li&gt;
      &lt;?php endwhile; endif; ?&gt;
      &lt;?php wp_reset_query(); ?&gt;
      ....
</pre>
<p>Searching for references to <kbd>wp_reset_query()</kbd> in the WordPress core files reveals only one instance of usage. That instance, oddly enough, is in the widget.php function for displaying the recent entries widget. Imagine that!</p>
<p>Reviewing the code, however, I made another discovery. Instead of using <kbd>query_posts()</kbd>, they create a new <a href="http://codex.wordpress.org/Function_Reference/WP_Query">WP_query object</a> to cycle through the posts and generate the recent entry list items. They use the new object in order to preserve the original query in the <em>main</em> loop, <a href="http://codex.wordpress.org/The_Loop">the loop</a> that displays your posts on page.  More importantly, the new object preserves the values of is_home() and other conditional tags &mdash; the use of wp_reset_query is really not needed here. Seems to be a left over from a previous version and not edited out of the core code.</p>
<p>In order to do this the <em>wordpress-ian</em> way, I recoded my sidebar widget for recent posts (<em>sans wp_reset_query</em>):</p>
<pre name="code" class="html">
<li class="widget">
<h3>Latest Blog Entries</h3>
<ul>
      &lt;?php $r = new WP_Query(array('showposts' => '10', 'what_to_show' => 'posts',
	         'nopaging' => 0, 'post_status' => 'publish', 'caller_get_posts' => 1)); ?&gt;
      &lt;?php if ($r->have_posts()) : while ($r->have_posts()) : $r->the_post(); ?&gt;
<li>&lt;a href="&lt;?php the_permalink() ?&gt;"&gt;&lt;?php the_title() ?&gt;&lt;/a&gt;</li>

      &lt;?php endwhile; endif; ?>
   </ul>
</li>
</pre>
<p>If you follow my logic in tracking down this little bugger, you will notice that my first resource to check was the conditional tag documentation in the Codex. Sadly, there is no mention of the consequences you will encounter by using query_posts() which alters the original page loop. </p>
<p>The WP support forum is filled with questions of &#8216;why is this not working&#8217;. Some go unanswered, some they fix and leave no reason how, some get the &#8216;review the conditonal tags documentation&#8217; buddy, and so on. It took a lot of searching to get a whiff of what I needed to do to correct the situation.</p>
<p>My suggestion is to at least insert a caveat into the documentation for conditional tags warning of what will happen to those tags if another query_posts() is called. This is the first place people will actually look for help after all. And it would probably save a lot of people&#8217;s time in the long run, not only the people seeking it, but the people offering a solution as well.</p>
<p><strong>UPDATE: </strong> The discussion for the creation of wp_reset_query() function can be found at <a href="http://trac.wordpress.org/ticket/4741">http://trac.wordpress.org/ticket/4741</a> if you are interested. Thanks to Stephen&#8217;s post <a href="http://striderweb.com/nerdaphernalia/2008/01/automated-indexes-and-wp_reset_query/">Automated Indexes and wp_reset_query()</a> at  Nerdaphernalia for pointing this out. </p>


<div class="shr-bookmarks shr-bookmarks-expand shr-bookmarks-center shr-bookmarks-bg-knowledge">
<ul class="socials">
		<li class="shr-delicious">
			<a href="http://www.shareaholic.com/api/share/?title=Undocumented+Wordpress+Query+Function%3A+wp_reset_query%28%29&amp;link=http://webstractions.com/wordpress/undocumented-wordpress-query-function-wp_reset_query/&amp;notes=Creating%20a%20conditional%20sidebar%20widget%20in%20Wordpress%20turned%20into%20an%20odyssey%20of%20discovery.%20How%20an%20obscure%20usage%20of%20query_posts%20will%20bork%20your%20is_home%28%29%20conditional%20tags.&amp;short_link=http://b2l.me/ap3ujv&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=2&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Share this on del.icio.us">Share this on del.icio.us</a>
		</li>
		<li class="shr-digg">
			<a href="http://www.shareaholic.com/api/share/?title=Undocumented+Wordpress+Query+Function%3A+wp_reset_query%28%29&amp;link=http://webstractions.com/wordpress/undocumented-wordpress-query-function-wp_reset_query/&amp;notes=Creating%20a%20conditional%20sidebar%20widget%20in%20Wordpress%20turned%20into%20an%20odyssey%20of%20discovery.%20How%20an%20obscure%20usage%20of%20query_posts%20will%20bork%20your%20is_home%28%29%20conditional%20tags.&amp;short_link=http://b2l.me/ap3ujv&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=3&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Digg this!">Digg this!</a>
		</li>
		<li class="shr-facebook">
			<a href="http://www.shareaholic.com/api/share/?title=Undocumented+Wordpress+Query+Function%3A+wp_reset_query%28%29&amp;link=http://webstractions.com/wordpress/undocumented-wordpress-query-function-wp_reset_query/&amp;notes=Creating%20a%20conditional%20sidebar%20widget%20in%20Wordpress%20turned%20into%20an%20odyssey%20of%20discovery.%20How%20an%20obscure%20usage%20of%20query_posts%20will%20bork%20your%20is_home%28%29%20conditional%20tags.&amp;short_link=http://b2l.me/ap3ujv&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=5&amp;tags=&amp;ctype=" rel="nofollow" title="Share this on Facebook">Share this on Facebook</a>
		</li>
		<li class="shr-gmail">
			<a href="http://www.shareaholic.com/api/share/?title=Undocumented+Wordpress+Query+Function%3A+wp_reset_query%28%29&amp;link=http://webstractions.com/wordpress/undocumented-wordpress-query-function-wp_reset_query/&amp;notes=Creating%20a%20conditional%20sidebar%20widget%20in%20Wordpress%20turned%20into%20an%20odyssey%20of%20discovery.%20How%20an%20obscure%20usage%20of%20query_posts%20will%20bork%20your%20is_home%28%29%20conditional%20tags.&amp;short_link=http://b2l.me/ap3ujv&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=52&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Email this via Gmail">Email this via Gmail</a>
		</li>
		<li class="shr-googlebuzz">
			<a href="http://www.shareaholic.com/api/share/?title=Undocumented+Wordpress+Query+Function%3A+wp_reset_query%28%29&amp;link=http://webstractions.com/wordpress/undocumented-wordpress-query-function-wp_reset_query/&amp;notes=Creating%20a%20conditional%20sidebar%20widget%20in%20Wordpress%20turned%20into%20an%20odyssey%20of%20discovery.%20How%20an%20obscure%20usage%20of%20query_posts%20will%20bork%20your%20is_home%28%29%20conditional%20tags.&amp;short_link=http://b2l.me/ap3ujv&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=257&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Post on Google Buzz">Post on Google Buzz</a>
		</li>
		<li class="shr-stumbleupon">
			<a href="http://www.shareaholic.com/api/share/?title=Undocumented+Wordpress+Query+Function%3A+wp_reset_query%28%29&amp;link=http://webstractions.com/wordpress/undocumented-wordpress-query-function-wp_reset_query/&amp;notes=Creating%20a%20conditional%20sidebar%20widget%20in%20Wordpress%20turned%20into%20an%20odyssey%20of%20discovery.%20How%20an%20obscure%20usage%20of%20query_posts%20will%20bork%20your%20is_home%28%29%20conditional%20tags.&amp;short_link=http://b2l.me/ap3ujv&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=38&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Stumble upon something good? Share it on StumbleUpon">Stumble upon something good? Share it on StumbleUpon</a>
		</li>
		<li class="shr-twitter">
			<a href="http://www.shareaholic.com/api/share/?title=Undocumented+Wordpress+Query+Function%3A+wp_reset_query%28%29&amp;link=http://webstractions.com/wordpress/undocumented-wordpress-query-function-wp_reset_query/&amp;notes=Creating%20a%20conditional%20sidebar%20widget%20in%20Wordpress%20turned%20into%20an%20odyssey%20of%20discovery.%20How%20an%20obscure%20usage%20of%20query_posts%20will%20bork%20your%20is_home%28%29%20conditional%20tags.&amp;short_link=http://b2l.me/ap3ujv&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=%24%7Btitle%7D+-+%24%7Bshort_link%7D&amp;service=7&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Tweet This!">Tweet This!</a>
		</li>
		<li class="shr-printfriendly">
			<a href="http://www.shareaholic.com/api/share/?title=Undocumented+Wordpress+Query+Function%3A+wp_reset_query%28%29&amp;link=http://webstractions.com/wordpress/undocumented-wordpress-query-function-wp_reset_query/&amp;notes=Creating%20a%20conditional%20sidebar%20widget%20in%20Wordpress%20turned%20into%20an%20odyssey%20of%20discovery.%20How%20an%20obscure%20usage%20of%20query_posts%20will%20bork%20your%20is_home%28%29%20conditional%20tags.&amp;short_link=http://b2l.me/ap3ujv&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=236&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Send this page to Print Friendly">Send this page to Print Friendly</a>
		</li>
</ul><div style="clear: both;"></div><div class="shr-getshr" style="visibility:hidden;font-size:10px !important"><a target="_blank" href="http://www.shareaholic.com/?src=pub">Get Shareaholic</a></div><div style="clear: both;"></div></div>

]]></content:encoded>
			<wfw:commentRss>http://webstractions.com/wordpress/undocumented-wordpress-query-function-wp_reset_query/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
	</channel>
</rss>

