<?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>wordpress avoid repeated posts Archives - CNERIS</title>
	<atom:link href="https://cneris.com/en/tag/wordpress-avoid-repeated-posts/feed/" rel="self" type="application/rss+xml" />
	<link>https://cneris.com/en/tag/wordpress-avoid-repeated-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 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>
