<?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>Weblog Tools Collection &#187; query_posts</title>
	<atom:link href="http://weblogtoolscollection.com/archives/tag/query_posts/feed/" rel="self" type="application/rss+xml" />
	<link>http://weblogtoolscollection.com</link>
	<description>Weblog Tools Blogging Tools Blog</description>
	<lastBuildDate>Mon, 13 Feb 2012 13:00:19 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Define Your Own WordPress Loop Using WP_Query</title>
		<link>http://weblogtoolscollection.com/archives/2008/04/13/define-your-own-wordpress-loop-using-wp_query/</link>
		<comments>http://weblogtoolscollection.com/archives/2008/04/13/define-your-own-wordpress-loop-using-wp_query/#comments</comments>
		<pubDate>Sun, 13 Apr 2008 08:00:00 +0000</pubDate>
		<dc:creator>Ronald Huereca</dc:creator>
				<category><![CDATA[HOW-TO]]></category>
		<category><![CDATA[WordPress FAQs]]></category>
		<category><![CDATA[WordPress Hack]]></category>
		<category><![CDATA[query_posts]]></category>
		<category><![CDATA[WordPress loop]]></category>
		<category><![CDATA[WP_QUERY]]></category>

		<guid isPermaLink="false">http://weblogtoolscollection.com/?p=3416</guid>
		<description><![CDATA[We all know what the WordPress Loop is right? If not, there are many great tutorials around the web that explain the WordPress Loop. One of the easiest ways to navigate and manipulate the loop is to use the function called query_posts. Nathan Rice calls it a WordPress developers best friend. When you use query_posts, however, you risk the following: Potential to interfere with plugins which make use of the Loop. Potential to invalidate WordPress conditional tags. Having to deal with resetting, rewinding, offsetting&#8230; I say skip query_posts. In a way you&#8217;ll still be using it, but the better (and sometimes easier) technique is to instantiate your own WP_Query object and create your own loop. Creating Your Own Loop With WP_Query The first step is to instantiate your own variable using the WP_Query class. What we&#8217;ll be doing in this example is creating a common feature on blogs, which is [...]]]></description>
			<content:encoded><![CDATA[<p>We all know what the <a href="http://www.themelab.com/2008/04/04/the-ultimate-guide-to-the-wordpress-loop/">WordPress Loop</a> is right?  If not, there are many great tutorials around the web that <a href="http://weblogtoolscollection.com/archives/2007/06/06/global-variables-and-the-wordpress-loop/">explain the WordPress Loop</a>.</p>
<p>One of the easiest ways to navigate and manipulate the loop is to use the function called <strong><a href="http://codex.wordpress.org/Template_Tags/query_posts">query_posts</a></strong>.  <a href="http://www.nathanrice.net/">Nathan Rice</a> calls it <a href="http://www.blogherald.com/2007/05/31/a-wordpress-developers-best-friend/">a WordPress developers best friend</a>.</p>
<p>When you use <strong>query_posts</strong>, however, you risk the following:</p>
<ul>
<li>Potential to interfere with plugins which make use of the Loop.</li>
<li>Potential to invalidate WordPress conditional tags.</li>
<li>Having to deal with resetting, rewinding, offsetting&#8230;</li>
</ul>
<p>I say skip <strong>query_posts</strong>.  In a way you&#8217;ll still be using it, but the better (and sometimes easier) technique is to instantiate your own <strong><a href="http://codex.wordpress.org/Function_Reference/WP_Query">WP_Query</a></strong> object and create your own loop.</p>
<h3>Creating Your Own Loop With WP_Query</h3>
<p>The first step is to instantiate your own variable using the WP_Query class.</p>
<p>What we&#8217;ll be doing in this example is creating a common feature on blogs, which is to display a list of the recent articles.</p>
<blockquote><p><code> </code></p>
<pre><code>&lt;?php
    $recentPosts = new WP_Query();
    $recentPosts-&gt;query('showposts=5');
?&gt;
</code></pre>
<p><code> </code></p></blockquote>
<p>All I&#8217;ve done in the above code is defined a variable named <strong>recentPosts</strong> and instantiated an instance of <strong>WP_Query</strong>.</p>
<p>I then used a method of <strong>WP_Query</strong> to start a query (pretty much the same thing as using <strong>query_posts</strong>).  You even use the <a href="http://codex.wordpress.org/Template_Tags/query_posts#Usage">same usage parameters</a> as <strong>query_posts</strong>.</p>
<p>Now it&#8217;s time to start our own loop:</p>
<blockquote><p><code> </code></p>
<pre><code>&lt;?php while ($recentPosts-&gt;have_posts()) : $recentPosts-&gt;the_post(); ?&gt;
   &lt;!-- do some stuff here --&gt;
&lt;?php endwhile; ?&gt;
</code></pre>
<p><code> </code></p></blockquote>
<p>Notice the use of the <strong>recentPosts</strong> variable to start the loop.  We utilize two methods of WP_Query, which is <strong>have_posts</strong> and <strong>the_post</strong>.  You can read more about those two methods on my article <a href="http://weblogtoolscollection.com/archives/2007/06/06/global-variables-and-the-wordpress-loop/">Global Variables and the WordPress Loop</a>.</p>
<p>The beauty of this is that once you are inside your own loop, you can use the <a href="http://codex.wordpress.org/Template_Tags#Post_tags">standard post tags</a>.</p>
<h3>The Full Code</h3>
<p>Here&#8217;s the full code for showing the last five recent posts using your own loop:</p>
<blockquote><p><code> </code></p>
<pre><code>&lt;h3&gt;Recent Articles&lt;/h3&gt;
&lt;ul&gt;
&lt;?php
    $recentPosts = new WP_Query();
    $recentPosts-&gt;query('showposts=5');
?&gt;
&lt;?php while ($recentPosts-&gt;have_posts()) : $recentPosts-&gt;the_post(); ?&gt;
    &lt;li&gt;&lt;a href="&lt;?php the_permalink() ?&gt;" rel="bookmark"&gt;&lt;?php the_title(); ?&gt;&lt;/a&gt;&lt;/li&gt;
&lt;?php endwhile; ?&gt;
&lt;/ul&gt;
</code></pre>
<p><code> </code></p></blockquote>
<h3>Update:  Using Pagination</h3>
<p>This comment is from <a href="http://www.anthologyoi.com">Aaron Harun</a> -</p>
<p>@Ron and Monika</p>
<p>If you use the query:</p>
<blockquote><p><code> </code></p>
<pre><code>$recentPosts-&gt;query('showposts=5'.'&amp;paged='.$paged);
</code></pre>
<p><code> </code></p></blockquote>
<p>it will automatically page the wury based on the page number passed<br />
through the url eg &#8220;/page/4&#8243;</p>
<p>However, this doesn&#8217;t work with the &#8220;<strong>post_nav_link</strong>&#8221; function because it<br />
only checks the $<strong>wp_query</strong> variable. To get around this you have to<br />
trick the function by switching $<strong>wp_query</strong> on it. Add the first block<br />
before your custom loop and the second after to achieve this effect.</p>
<blockquote><p><code> </code></p>
<pre><code>&lt;?php //query_posts('paged='.$paged);
$temp = $wp_query;
$wp_query= null;
   $wp_query = new WP_Query();
   $wp_query-&gt;query('showposts=5'.'&amp;paged='.$paged);
?&gt;
</code></pre>
<p><code> </code></p></blockquote>
<blockquote><p><code> </code></p>
<pre><code>&lt;?php $wp_query = null; $wp_query = $temp;?&gt;
</code></pre>
<p><code> </code></p></blockquote>
<p><em>Thanks Aaron for the contribution.</em></p>
<h3>Conclusion</h3>
<p>Defining your own loop using WP_Query is an easy way to run your own custom queries without interfering with the default Loop.  It&#8217;s also a great way to run multiple loops that are completely independent of each other.</p>
<p>WordPress Resources mentioned:</p>
<ul>
<li><a href="http://codex.wordpress.org/Function_Reference/WP_Query">WP_Query</a></li>
<li><a href="http://codex.wordpress.org/Template_Tags/query_posts">query_posts</a></li>
<li><a href="http://www.themelab.com/2008/04/04/the-ultimate-guide-to-the-wordpress-loop/">The Loop</a></li>
<li><a href="http://codex.wordpress.org/Template_Tags#Post_tags">Post Template Tags</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://weblogtoolscollection.com/archives/2008/04/13/define-your-own-wordpress-loop-using-wp_query/feed/</wfw:commentRss>
		<slash:comments>76</slash:comments>
		</item>
	</channel>
</rss>
<!-- This Quick Cache file was built for (  weblogtoolscollection.com/archives/tag/query_posts/feed/ ) in 0.79955 seconds, on Feb 14th, 2012 at 9:18 am UTC. -->
<!-- This Quick Cache file will automatically expire ( and be re-built automatically ) on Feb 14th, 2012 at 10:18 am UTC -->
