<?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>Ben Huson&#187; WordPress Loop</title>
	<atom:link href="http://www.benhuson.co.uk/tag/wordpress-loop/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.benhuson.co.uk</link>
	<description>Web Designer and Developer</description>
	<lastBuildDate>Wed, 11 Aug 2010 21:23:05 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>How to ensure WordPress template tags work properly when using multiple WordPress Loops</title>
		<link>http://www.benhuson.co.uk/2009/12/08/template-tags-when-using-multiple-wordpress-loops/</link>
		<comments>http://www.benhuson.co.uk/2009/12/08/template-tags-when-using-multiple-wordpress-loops/#comments</comments>
		<pubDate>Tue, 08 Dec 2009 22:50:08 +0000</pubDate>
		<dc:creator>Ben</dc:creator>
				<category><![CDATA[Web Developer]]></category>
		<category><![CDATA[WordPress]]></category>
		<category><![CDATA[have_posts()]]></category>
		<category><![CDATA[the_post()]]></category>
		<category><![CDATA[WordPress Loop]]></category>
		<category><![CDATA[WP_Query]]></category>

		<guid isPermaLink="false">http://www.benhuson.co.uk/?p=1276</guid>
		<description><![CDATA[There are many occasions when you may want to create multiple WordPress Loops &#8211; instructions on how to do this can be found on the WordPress Codex. If you need to keep the original query, it is suggested you create you second loop by creating a new query object. &#60;?php $my_query = new WP_Query&#40;'category_name=special_cat&#38;posts_per_page=10'&#41;; ?&#62; &#60;?php [...]]]></description>
			<content:encoded><![CDATA[<p><strong>There are many occasions when you may want to create multiple WordPress Loops &#8211; instructions on how to do this can be found on the </strong><a href="http://codex.wordpress.org/The_Loop#Multiple_Loops_Example_1"><strong>WordPress Codex</strong></a><strong>.</strong></p>
<p>If you need to keep the original query, it is suggested you create you second loop by creating a new query object.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #000088;">$my_query</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> WP_Query<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'category_name=special_cat&amp;posts_per_page=10'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$my_query</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">have_posts</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> <span style="color: #000088;">$my_query</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">the_post</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
   &lt;!-- Do special_cat stuff... --&gt;
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">endwhile</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>This works fine, but if you use more template tags after this loop, they display the content from the last post in the $my_query loop. So, if you want to reference the original loop how should you go about it?</p>
<p><span id="more-1276"></span></p>
<h2>What&#8217;s going on?</h2>
<p>In the above example the $my_query variable is an instance of the WP_Query object containing the results of the custom query.</p>
<p>The &#8216;while&#8217; loop iterates through all the results of our query. Each time $my_query-&gt;have_posts() is called it checks wether the next post in the loop exists and if it does the loop continues to run.</p>
<p>For each post in the loop the $my_query-&gt;the_post() function is called. This populates the global $post variable with the post data which can then be accessed directly through the $post variable or using WordPress template tags like the_title() and the_content();</p>
<p>After the $my_query loop has finished <strong>the global $post variable remains populated</strong> with the last post from the $my_query loop so any template tags used will use this post&#8217;s data, not the data from the post before we started our custom loop.</p>
<h2>So what should we do?</h2>
<p>Because we created a new query $my_query, the previous query still exists in the main $wp_query variable, so all we have to do is repopulate the $post variable using $wp_query and setup the post data again.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000088;">$post</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$wp_query</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">post</span><span style="color: #339933;">;</span>
setup_postdata<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$post</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<h2>Could this be made more easy?</h2>
<p>Not really, but maybe a WordPress template tag to do this should be added to WordPress core &#8211; maybe somthing like refresh_post(). This can then be documented in the Codex to provide an &#8216;official&#8217; way of handling multiple loops.</p>
<p>What do you think?</p>
]]></content:encoded>
			<wfw:commentRss>http://www.benhuson.co.uk/2009/12/08/template-tags-when-using-multiple-wordpress-loops/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Page Caching using disk (enhanced)

Served from: benhuson.co.uk @ 2010-09-10 17:45:12 -->