<?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>ajaxtime.com &#187; function</title>
	<atom:link href="http://www.ajaxtime.com/tag/function/feed" rel="self" type="application/rss+xml" />
	<link>http://www.ajaxtime.com</link>
	<description>Just another WordPress weblog</description>
	<lastBuildDate>Tue, 13 Apr 2010 14:41:40 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Define Your Own WordPress Loop Using WP_Query</title>
		<link>http://www.ajaxtime.com/define-your-own-wordpress-loop-using-wp_query.html</link>
		<comments>http://www.ajaxtime.com/define-your-own-wordpress-loop-using-wp_query.html#comments</comments>
		<pubDate>Thu, 16 Apr 2009 08:24:43 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[wordpress]]></category>
		<category><![CDATA[function]]></category>

		<guid isPermaLink="false">http://www.ajaxtime.com/?p=93</guid>
		<description><![CDATA[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.]]></description>
			<content:encoded><![CDATA[<p>We all know what the 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…</li>
</ul>
<p>I say skip <strong>query_posts</strong>. In a way you’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’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> </p>
<pre>&lt;?php
    $recentPosts = new WP_Query();
    $recentPosts-&gt;query('showposts=5');
?&gt;</pre>
<p> </p></blockquote>
<p>All I’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’s time to start our own loop:</p>
<blockquote><p> </p>
<pre>&lt;?php while ($recentPosts-&gt;have_posts()) : $recentPosts-&gt;the_post(); ?&gt;
   &lt;!-- do some stuff here --&gt;
&lt;?php endwhile; ?&gt;</pre>
<p> </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’s the full code for showing the last five recent posts using your own loop:</p>
<blockquote><p> </p>
<pre>&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;</pre>
<p> </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> </p>
<pre>$recentPosts-&gt;query('showposts=5'.'&amp;paged='.$paged);</pre>
<p> </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 “<strong>post_nav_link</strong>” 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> </p>
<pre>&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;</pre>
<p> </p></blockquote>
<blockquote><p> </p>
<pre>&lt;?php $wp_query = null; $wp_query = $temp;?&gt;</pre>
<p> </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’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>
<p>Read more: <a href="http://weblogtoolscollection.com/archives/2008/04/13/define-your-own-wordpress-loop-using-wp_query/#ixzz0CpIVsvPU&amp;B">http://weblogtoolscollection.com/archives/2008/04/13/define-your-own-wordpress-loop-using-wp_query/#ixzz0CpIVsvPU&amp;B</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ajaxtime.com/define-your-own-wordpress-loop-using-wp_query.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
