<?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; the loop</title>
	<atom:link href="http://weblogtoolscollection.com/archives/tag/the-loop/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>How To:  Avoid Duplicate Posts</title>
		<link>http://weblogtoolscollection.com/archives/2008/05/17/how-to-avoid-duplicate-posts/</link>
		<comments>http://weblogtoolscollection.com/archives/2008/05/17/how-to-avoid-duplicate-posts/#comments</comments>
		<pubDate>Sun, 18 May 2008 01:58:30 +0000</pubDate>
		<dc:creator>Ronald Huereca</dc:creator>
				<category><![CDATA[HOW-TO]]></category>
		<category><![CDATA[WordPress]]></category>
		<category><![CDATA[WordPress FAQs]]></category>
		<category><![CDATA[WordPress Hack]]></category>
		<category><![CDATA[downloads]]></category>
		<category><![CDATA[loop]]></category>
		<category><![CDATA[the loop]]></category>

		<guid isPermaLink="false">http://weblogtoolscollection.com/?p=3563</guid>
		<description><![CDATA[A reader writes in: I&#8217;m developing a new theme and I&#8217;m having trouble getting duplicate posts from showing when running two loops (one standard loop and one from a specific category). Even when I copied the specific code from directly from the codex, it was not working. The Codex article the reader mentioned was regarding the Loop. Although the example shows how to avoid a single duplicate post, it doesn&#8217;t show how to avoid duplicating multiple posts. Here&#8217;s how to show two individual loops without duplicating posts in either loop. Step 1: Add a &#8216;posts_where&#8217; Function A WordPress filter is needed to accomplish this, and we&#8217;re going to be tapping into the &#8216;posts_where&#8216; filter. The reason being is we need to modify the query used for the loop and exclude some posts. Here&#8217;s the function we&#8217;ll be using called post_strip: function post_strip($where) { global $myPosts, $wpdb; $where .= &#34; AND [...]]]></description>
			<content:encoded><![CDATA[<p>A reader writes in:</p>
<blockquote><p>
I&#8217;m developing a new theme and I&#8217;m having trouble getting duplicate posts from showing when running two loops (one standard loop and one from a specific category). Even when I copied the specific code from directly from the codex, it was not working.
</p></blockquote>
<p>The Codex article the reader mentioned <a href="http://codex.wordpress.org/The_Loop#Multiple_Loops_in_Action">was regarding the Loop</a>.  Although the example shows how to avoid a single duplicate post, it doesn&#8217;t show how to avoid duplicating multiple posts.</p>
<p>Here&#8217;s how to show two individual loops without duplicating posts in either loop.</p>
<h3>Step 1:  Add a &#8216;posts_where&#8217; Function</h3>
<p>A <a href="http://codex.wordpress.org/Plugin_API/Filter_Reference">WordPress filter</a> is needed to accomplish this, and we&#8217;re going to be tapping into the &#8216;<strong>posts_where</strong>&#8216; filter.</p>
<p>The reason being is we need to modify the query used for the loop and exclude some posts.  </p>
<p>Here&#8217;s the function we&#8217;ll be using called <strong>post_strip</strong>:</p>
<blockquote><p><code>
<pre>
function post_strip($where) {
	global $myPosts, $wpdb;
	$where .= &quot; AND $wpdb-&gt;posts.ID not in($myPosts) &quot;;
	return $where;
}
</pre>
<p></code></p></blockquote>
<p>In the above code, I use a global variable called <strong>$myPosts</strong>, which is comma-separated string of post IDs to exclude.</p>
<h3>Step 2:  Start the First Loop</h3>
<p>Within this first loop we&#8217;ll be keeping track of the post IDs being used.  Nothing fancy is being done here.  We&#8217;re just pulling the last five posts posted.</p>
<blockquote><p><code>
<pre>
&lt;?php
global $myPosts;
$myPosts = &#39;&#39;;
?&gt;
&lt;div&gt;
&lt;?php
$my_query = new WP_Query();
$my_query-&gt;query(&#39;showposts=5&#39;);
if ($my_query-&gt;have_posts()) : while ($my_query-&gt;have_posts()) : $my_query-&gt;the_post(); ?&gt;
&lt;?php $myPosts .= $post-&gt;ID . &quot;,&quot;; ?&gt;
&lt;div&gt;&lt;?php the_title(); ?&gt;&lt;/div&gt;
&lt;!-- Post Stuff --&gt;
&lt;?php endwhile; endif; ?&gt;
&lt;/div&gt;
</pre>
<p></code></p></blockquote>
<p>Pay special attention to the <strong>$myPosts</strong> variable, which is used to keep track of all of the post IDs.  </p>
<h3>Step 3:  Add the Filter</h3>
<p>We&#8217;ll now need to add a <strong>posts_where</strong> filter for the second loop.  This filter will use the <strong>post_strip</strong> function we started in Step 1.  </p>
<blockquote><p><code>
<pre>
&lt;?php add_filter(&#39;posts_where&#39;, &#39;post_strip&#39;); ?&gt;
</pre>
<p></code></p></blockquote>
<h3>Step 4:  Start the Second Loop</h3>
<p>The second loop is a repeat of the first loop to demonstrate that the posts are not being duplicated.  The second loop uses a different loop technique since paging isn&#8217;t necessary.</p>
<blockquote><p><code>
<pre>
&lt;div&gt;
&lt;?php
$my_query = new WP_Query(&#39;showposts=5&#39;);
while ($my_query-&gt;have_posts()) : $my_query-&gt;the_post();?&gt;
&lt;div&gt;&lt;?php the_title(); ?&gt;&lt;/div&gt;

&lt;!-- Post Stuff --&gt;

&lt;?php endwhile; ?&gt;
&lt;/div&gt;
</pre>
<p></code></p></blockquote>
<h3>Step 5:  Remove the Filter</h3>
<p>The filter we added in Step 3 now needs to be removed.  </p>
<blockquote><p><code>
<pre>
&lt;?php remove_filter(&#39;posts_where&#39;, &#39;post_strip&#39;); ?&gt;
</pre>
<p></code></p></blockquote>
<h3>Step 6:  Admire the Results</h3>
<p class="screenshot"><img src="http://weblogtoolscollection.com/b2-img/2008/05/before-filter-duplicate-posts.jpeg" alt="Before Duplicates Being Shown" width="118" height="224" /><br/>Before &#8211; Duplicates Being Shown</p>
<p class="screenshot"><img src="http://weblogtoolscollection.com/b2-img/2008/05/after-filter-duplicate-posts.jpeg" alt="After - Duplicates Removed" width="319" height="219" /><br/>After &#8211; Duplicates Removed</p>
<h3>Downloadable Code</h3>
<p>Here is a <a href="http://weblogtoolscollection.com/b2-img/2008/05/indexphp1.zip">sample index.php</a> for download.</p>
]]></content:encoded>
			<wfw:commentRss>http://weblogtoolscollection.com/archives/2008/05/17/how-to-avoid-duplicate-posts/feed/</wfw:commentRss>
		<slash:comments>28</slash:comments>
		</item>
	</channel>
</rss>
<!-- This Quick Cache file was built for (  weblogtoolscollection.com/archives/tag/the-loop/feed/ ) in 0.59274 seconds, on Feb 14th, 2012 at 8:14 am UTC. -->
<!-- This Quick Cache file will automatically expire ( and be re-built automatically ) on Feb 14th, 2012 at 9:14 am UTC -->
