<?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>avoid repeating posts Archives - CNERIS</title>
	<atom:link href="https://cneris.com/en/tag/avoid-repeating-posts/feed/" rel="self" type="application/rss+xml" />
	<link>https://cneris.com/en/tag/avoid-repeating-posts/</link>
	<description></description>
	<lastBuildDate>Mon, 01 Jul 2024 19:41:10 +0000</lastBuildDate>
	<language>en-GB</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.8.1</generator>
	<item>
		<title>Avoid repeating posts in WordPress</title>
		<link>https://cneris.com/en/avoid-repeating-posts-in-wordpress/</link>
					<comments>https://cneris.com/en/avoid-repeating-posts-in-wordpress/#respond</comments>
		
		<dc:creator><![CDATA[admin]]></dc:creator>
		<pubDate>Mon, 01 Jul 2024 16:56:43 +0000</pubDate>
				<category><![CDATA[Wordpress]]></category>
		<category><![CDATA[avoid repeating posts]]></category>
		<category><![CDATA[wordpress]]></category>
		<guid isPermaLink="false">https://cneris.com/?p=1561</guid>

					<description><![CDATA[<p>To avoid showing the same WordPress post in multiple loops, you can use a couple of methods. The most common approach is to keep track of the post IDs that have already been displayed and then exclude those posts from subsequent loops. Here’s a step-by-step guide on how to achieve this: Method 1: Using Global [...]</p>
<p>The post <a href="https://cneris.com/en/avoid-repeating-posts-in-wordpress/">Avoid repeating posts in WordPress</a> appeared first on <a href="https://cneris.com/en">CNERIS</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>To avoid showing the same WordPress post in multiple loops, you can use a couple of methods. The most common approach is to keep track of the post IDs that have already been displayed and then exclude those posts from subsequent loops. Here’s a step-by-step guide on how to achieve this:</p>
<h3>Method 1: Using Global Variable</h3>
<ol>
<li><strong>Initialize an Array to Store Displayed Post IDs</strong>Before your first loop, initialize an empty array to keep track of the post IDs.
<div class="dark bg-gray-950 rounded-md border-[0.5px] border-token-border-medium">
<div class="flex items-center relative text-token-text-secondary bg-token-main-surface-secondary px-4 py-2 text-xs font-sans justify-between rounded-t-md">
<p>php</p>
<div class="flex items-center"></div>
</div>
<div class="overflow-y-auto p-4" dir="ltr"><code class="!whitespace-pre hljs language-php"><span class="hljs-keyword">global</span> <span class="hljs-variable">$do_not_duplicate</span>;<br />
<span class="hljs-variable">$do_not_duplicate</span> = <span class="hljs-keyword">array</span>();<br />
</code></div>
</div>
</li>
<li><strong>First Loop</strong>In your first loop, store the post IDs of the displayed posts in the global array.
<div class="dark bg-gray-950 rounded-md border-[0.5px] border-token-border-medium">
<div class="flex items-center relative text-token-text-secondary bg-token-main-surface-secondary px-4 py-2 text-xs font-sans justify-between rounded-t-md">
<p>php</p>
<div class="flex items-center"></div>
</div>
<div class="overflow-y-auto p-4" dir="ltr"><code class="!whitespace-pre hljs language-php"><code class="!whitespace-pre hljs language-php"><span class="hljs-meta">&lt;?php</span><br />
<span class="hljs-keyword">global</span> <span class="hljs-variable">$do_not_duplicate</span>;<br />
<span class="hljs-variable">$query1</span> = <span class="hljs-keyword">new</span> <span class="hljs-title class_">WP_Query</span>(<span class="hljs-keyword">array</span>(<br />
<span class="hljs-string">'posts_per_page'</span> =&gt; 5,<br />
));</code></code><span class="hljs-keyword">if</span> (<span class="hljs-variable">$query1</span>-&gt;<span class="hljs-title function_ invoke__">have_posts</span>()) :<br />
<span class="hljs-keyword">while</span> (<span class="hljs-variable">$query1</span>-&gt;<span class="hljs-title function_ invoke__">have_posts</span>()) : <span class="hljs-variable">$query1</span>-&gt;<span class="hljs-title function_ invoke__">the_post</span>();<br />
<span class="hljs-variable">$do_not_duplicate</span>[] = <span class="hljs-title function_ invoke__">get_the_ID</span>();<br />
<span class="hljs-comment">// Your loop code here</span><br />
<span class="hljs-keyword">endwhile</span>;<br />
<span class="hljs-title function_ invoke__">wp_reset_postdata</span>();<br />
<span class="hljs-keyword">endif</span>;<br />
<span class="hljs-meta">?&gt;</span></p>
</div>
</div>
</li>
<li><strong>Second Loop</strong>In your second loop, exclude the post IDs stored in the global array.
<div class="dark bg-gray-950 rounded-md border-[0.5px] border-token-border-medium">
<div class="flex items-center relative text-token-text-secondary bg-token-main-surface-secondary px-4 py-2 text-xs font-sans justify-between rounded-t-md">
<p>php</p>
<div class="flex items-center"></div>
</div>
<div class="overflow-y-auto p-4" dir="ltr"><code class="!whitespace-pre hljs language-php"><code class="!whitespace-pre hljs language-php"><span class="hljs-meta">&lt;?php</span><br />
<span class="hljs-keyword">global</span> <span class="hljs-variable">$do_not_duplicate</span>;<br />
<span class="hljs-variable">$query2</span> = <span class="hljs-keyword">new</span> <span class="hljs-title class_">WP_Query</span>(<span class="hljs-keyword">array</span>(<br />
<span class="hljs-string">'posts_per_page'</span> =&gt; 5,<br />
<span class="hljs-string">'post__not_in'</span> =&gt; <span class="hljs-variable">$do_not_duplicate</span>,<br />
));</code></code><span class="hljs-keyword">if</span> (<span class="hljs-variable">$query2</span>-&gt;<span class="hljs-title function_ invoke__">have_posts</span>()) :<br />
<span class="hljs-keyword">while</span> (<span class="hljs-variable">$query2</span>-&gt;<span class="hljs-title function_ invoke__">have_posts</span>()) : <span class="hljs-variable">$query2</span>-&gt;<span class="hljs-title function_ invoke__">the_post</span>();<br />
<span class="hljs-comment">// Your loop code here</span><br />
<span class="hljs-keyword">endwhile</span>;<br />
<span class="hljs-title function_ invoke__">wp_reset_postdata</span>();<br />
<span class="hljs-keyword">endif</span>;<br />
<span class="hljs-meta">?&gt;</span></p>
</div>
</div>
</li>
</ol>
<h3>Method 2: Using Transients for More Complex Scenarios</h3>
<p>If you have more complex scenarios or need to persist the exclusion across different parts of your site, you might consider using WordPress transients.</p>
<ol>
<li><strong>Set Up a Transient</strong>Before your loops, check if a transient exists and use it. If not, initialize an empty array.
<div class="dark bg-gray-950 rounded-md border-[0.5px] border-token-border-medium">
<div class="flex items-center relative text-token-text-secondary bg-token-main-surface-secondary px-4 py-2 text-xs font-sans justify-between rounded-t-md">
<p>php</p>
<div class="flex items-center"></div>
</div>
<div class="overflow-y-auto p-4" dir="ltr"><code class="!whitespace-pre hljs language-php"><span class="hljs-variable">$do_not_duplicate</span> = <span class="hljs-title function_ invoke__">get_transient</span>(<span class="hljs-string">'excluded_posts'</span>) ? <span class="hljs-title function_ invoke__">get_transient</span>(<span class="hljs-string">'excluded_posts'</span>) : <span class="hljs-keyword">array</span>();<br />
</code></div>
</div>
</li>
<li><strong>First Loop</strong>Store the post IDs in the transient after your first loop.
<div class="dark bg-gray-950 rounded-md border-[0.5px] border-token-border-medium">
<div class="flex items-center relative text-token-text-secondary bg-token-main-surface-secondary px-4 py-2 text-xs font-sans justify-between rounded-t-md">
<p>php</p>
<div class="flex items-center"></div>
</div>
<div class="overflow-y-auto p-4" dir="ltr"><code class="!whitespace-pre hljs language-php"><code class="!whitespace-pre hljs language-php"><span class="hljs-meta">&lt;?php</span><br />
<span class="hljs-variable">$query1</span> = <span class="hljs-keyword">new</span> <span class="hljs-title class_">WP_Query</span>(<span class="hljs-keyword">array</span>(<br />
<span class="hljs-string">'posts_per_page'</span> =&gt; 5,<br />
));</code></code><span class="hljs-keyword">if</span> (<span class="hljs-variable">$query1</span>-&gt;<span class="hljs-title function_ invoke__">have_posts</span>()) :<br />
<span class="hljs-keyword">while</span> (<span class="hljs-variable">$query1</span>-&gt;<span class="hljs-title function_ invoke__">have_posts</span>()) : <span class="hljs-variable">$query1</span>-&gt;<span class="hljs-title function_ invoke__">the_post</span>();<br />
<span class="hljs-variable">$do_not_duplicate</span>[] = <span class="hljs-title function_ invoke__">get_the_ID</span>();<br />
<span class="hljs-comment">// Your loop code here</span><br />
<span class="hljs-keyword">endwhile</span>;<br />
<span class="hljs-title function_ invoke__">wp_reset_postdata</span>();<br />
<span class="hljs-title function_ invoke__">set_transient</span>(<span class="hljs-string">&#8216;excluded_posts&#8217;</span>, <span class="hljs-variable">$do_not_duplicate</span>, HOUR_IN_SECONDS);<br />
<span class="hljs-keyword">endif</span>;<br />
<span class="hljs-meta">?&gt;</span></p>
</div>
</div>
</li>
<li><strong>Second Loop</strong>Exclude the post IDs stored in the transient in your second loop.
<div class="dark bg-gray-950 rounded-md border-[0.5px] border-token-border-medium">
<div class="flex items-center relative text-token-text-secondary bg-token-main-surface-secondary px-4 py-2 text-xs font-sans justify-between rounded-t-md">
<p>php</p>
<div class="flex items-center"></div>
</div>
<div class="overflow-y-auto p-4" dir="ltr"><code class="!whitespace-pre hljs language-php"><code class="!whitespace-pre hljs language-php"><span class="hljs-meta">&lt;?php</span><br />
<span class="hljs-variable">$query2</span> = <span class="hljs-keyword">new</span> <span class="hljs-title class_">WP_Query</span>(<span class="hljs-keyword">array</span>(<br />
<span class="hljs-string">'posts_per_page'</span> =&gt; 5,<br />
<span class="hljs-string">'post__not_in'</span> =&gt; <span class="hljs-variable">$do_not_duplicate</span>,<br />
));</code></code><span class="hljs-keyword">if</span> (<span class="hljs-variable">$query2</span>-&gt;<span class="hljs-title function_ invoke__">have_posts</span>()) :<br />
<span class="hljs-keyword">while</span> (<span class="hljs-variable">$query2</span>-&gt;<span class="hljs-title function_ invoke__">have_posts</span>()) : <span class="hljs-variable">$query2</span>-&gt;<span class="hljs-title function_ invoke__">the_post</span>();<br />
<span class="hljs-comment">// Your loop code here</span><br />
<span class="hljs-keyword">endwhile</span>;<br />
<span class="hljs-title function_ invoke__">wp_reset_postdata</span>();<br />
<span class="hljs-keyword">endif</span>;<br />
<span class="hljs-meta">?&gt;</span></p>
</div>
</div>
</li>
</ol>
<p>By following these methods, you can avoid displaying the same post in multiple loops on your WordPress site. The first method using a global variable is straightforward and works well for simple use cases. The second method using transients is more suitable for complex scenarios where exclusions need to persist across different parts of the site or for a longer duration.</p>
<p>The post <a href="https://cneris.com/en/avoid-repeating-posts-in-wordpress/">Avoid repeating posts in WordPress</a> appeared first on <a href="https://cneris.com/en">CNERIS</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://cneris.com/en/avoid-repeating-posts-in-wordpress/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Avoid showing same WordPress post in multiple loops</title>
		<link>https://cneris.com/en/avoid-showing-same-wordpress-post-in-multiple-loops/</link>
					<comments>https://cneris.com/en/avoid-showing-same-wordpress-post-in-multiple-loops/#respond</comments>
		
		<dc:creator><![CDATA[admin]]></dc:creator>
		<pubDate>Mon, 01 Jul 2024 16:53:41 +0000</pubDate>
				<category><![CDATA[Wordpress]]></category>
		<category><![CDATA[avoid repeating posts]]></category>
		<category><![CDATA[wordpress avoid repeated posts]]></category>
		<category><![CDATA[wordpress avoid repeating posts]]></category>
		<guid isPermaLink="false">https://cneris.com/?p=1558</guid>

					<description><![CDATA[<p>Your WordPress theme might have multiple loops in the page. For example, there may be a ‘Featured’ section at the top of your homepage, and a ‘Recent Posts’ section below that. These are separate loops, but could contain the same post. For example, your most recent post might also be in the Featured category. You [...]</p>
<p>The post <a href="https://cneris.com/en/avoid-showing-same-wordpress-post-in-multiple-loops/">Avoid showing same WordPress post in multiple loops</a> appeared first on <a href="https://cneris.com/en">CNERIS</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>Your WordPress theme might have multiple loops in the page. For example, there may be a ‘Featured’ section at the top of your homepage, and a ‘Recent Posts’ section below that.</p>
<p>These are separate loops, but could contain the same post. For example, your most recent post might also be in the Featured category.</p>
<p>You only want the post to appear once. How do you manage it?</p>
<p>Avoid showing duplicate WordPress posts<br />
The answer is surprisingly simple. Put the following in your functions.php.</p>
<p><em>&lt;?php</em><br />
<em>add_filter(&#8216;post_link&#8217;, &#8216;track_displayed_posts&#8217;);</em><br />
<em>add_action(&#8216;pre_get_posts&#8217;,&#8217;remove_already_displayed_posts&#8217;);</em></p>
<p><em>$displayed_posts = [];</em></p>
<p><em>function track_displayed_posts($url) {</em><br />
<em>global $displayed_posts;</em><br />
<em>$displayed_posts[] = get_the_ID();</em><br />
<em>return $url; // don&#8217;t mess with the url</em><br />
<em>}</em></p>
<p><em>function remove_already_displayed_posts($query) {</em><br />
<em>global $displayed_posts;</em><br />
<em>$query-&gt;set(&#8216;post__not_in&#8217;, $displayed_posts);</em><br />
<em>}</em></p>
<p>We only want to hide posts which have been displayed already – so we hook into the post_link() function call and make a note of the post we’re displaying.</p>
<p>We then want to hook into any new WordPress query to tell the query to not include any of the posts we’ve already seen. We do this by hooking into the pre_get_posts function and passing it our list of already-displayed posts.</p>
<p>The post <a href="https://cneris.com/en/avoid-showing-same-wordpress-post-in-multiple-loops/">Avoid showing same WordPress post in multiple loops</a> appeared first on <a href="https://cneris.com/en">CNERIS</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://cneris.com/en/avoid-showing-same-wordpress-post-in-multiple-loops/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
