<?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>PSD to Wordpress &#187; recent posts</title> <atom:link href="http://www.sramekdesign.com/tag/recent-posts/feed/" rel="self" type="application/rss+xml" /><link>http://www.sramekdesign.com</link> <description>Get your PSD sliced into the XHTML valid Wordpress theme. SramekDesign offers cheap and fast PSD to Wordpress conversion service.</description> <lastBuildDate>Wed, 20 Apr 2011 04:33:37 +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>Display recent and related posts without duplicating each other</title><link>http://www.sramekdesign.com/wordpress/display-recent-and-related-posts-without-duplicating-each-other/</link> <comments>http://www.sramekdesign.com/wordpress/display-recent-and-related-posts-without-duplicating-each-other/#comments</comments> <pubDate>Tue, 02 Mar 2010 08:22:57 +0000</pubDate> <dc:creator>Tom</dc:creator> <category><![CDATA[Wordpress]]></category> <category><![CDATA[do not duplicate]]></category> <category><![CDATA[function]]></category> <category><![CDATA[recent posts]]></category> <category><![CDATA[related posts]]></category> <category><![CDATA[WP_Query]]></category><guid
isPermaLink="false">http://sramekdesign.com/?p=496</guid> <description><![CDATA[My client asked me to implement lists of recent and related posts below a single post without duplicating each other. What do we have to do? Create 2 instances of WP_Query class ($recent_query and $tags_query) By the help of these instances we have to create loops for recent and related posts IDs of the recent [...]]]></description> <content:encoded><![CDATA[<p>My client asked me to implement lists of recent and related posts below a single post without duplicating each other.<div
class="clear"></div><p><img
src="http://sramekdesign.com/wp-content/uploads/2010/03/re-rel.jpg" alt="recent and related posts" title="re-rel" class="alignleft size-full wp-image-497" /><div
class="clear"></div><h3>What do we have to do?</h3><ol><li>Create 2 instances of <a
rel="nofollow" href="http://codex.wordpress.org/Function_Reference/WP_Query">WP_Query class</a> ($recent_query and $tags_query)</li><li>By the help of these instances we have to create loops for recent and related posts</li><li>IDs of the recent posts must be stored in an array for later use</li><li>Implement displaying of the related posts based on tags</li><li>In the loop for tags we have to exclude IDs of recent posts from displaying</li></ol><p>This tutorial has two parts:</p><ul><li>Recent posts</li><li>Related posts</li></ul><h4>Recent posts</h4><p>This is how full code for recent posts looks:</p><pre class="brush: php; html-script: true; title: ; notranslate">
&lt;?php
/**
 * recent posts
 */
?&gt;

&lt;h2&gt;recent 5 posts&lt;/h2&gt;
&lt;?php $recent_query = new WP_Query(); ?&gt;
&lt;?php $recent_query-&gt;query('showposts=5'); $ids = array(); ?&gt;
&lt;ul&gt;
&lt;?php while($recent_query-&gt;have_posts()) : $recent_query-&gt;the_post();
 $ids[] = get_the_ID(); // we have to store IDs in the array for later use
?&gt;
	&lt;li&gt;
            &lt;a href=&quot;&lt;?php the_permalink(); ?&gt;&quot;&gt;
            &lt;?php the_title(); ?&gt;
            &lt;/a&gt;
       &lt;/li&gt;
&lt;?php endwhile; ?&gt;
&lt;/ul&gt;
</pre><p>Now let&#8217;s go step by step:<br
/> The first we have to create instance of the WP_Query() class. Its references retain a lot of info that we can use later. More info about this class you can find in the <a
rel="nofollow" href="http://codex.wordpress.org/Function_Reference/WP_Query">function reference.</a></p><pre class="brush: php; title: ; notranslate">
&lt;?php $recent_query = new WP_Query(); ?&gt;
</pre><p>With the instance of WP_QUery we can query posts. We also have  to create an array $ids that we&#8217;ll use later for storing IDs of the rcent posts we displayed.</p><pre class="brush: php; title: ; notranslate">
&lt;?php
$recent_query-&gt;query('showposts=5'); // we are going to display 5 posts
$ids = array(); // create array we will use for storing recent posts' IDs
?&gt;
</pre><p>Using $recent_query object we can run a loop to display last 5 posts. We also have to get the ID of each post and store it in $id array. Note that the loop is inside ul tags i.e we want to repeat only list items with unique permalinks, not whole list.</p><pre class="brush: php; html-script: true; title: ; notranslate">
&lt;ul&gt;
&lt;?php while($recent_query-&gt;have_posts()) : $recent_query-&gt;the_post();
 $ids[] = get_the_ID(); // we have to store IDs in the array for later use
?&gt;
	&lt;li&gt;
            &lt;a href=&quot;&lt;?php the_permalink(); ?&gt;&quot;&gt;
            &lt;?php the_title(); ?&gt;
            &lt;/a&gt;
       &lt;/li&gt;
&lt;?php endwhile; ?&gt;
&lt;/ul&gt;
</pre><h4>Related posts</h4><p>Here is a code of displaying of the related posts based on tags.</p><pre class="brush: php; html-script: true; title: ; notranslate">
&lt;?php
/**
 * related posts
 */

$tags = wp_get_post_tags( array('showposts' =&gt; 5, 'post__not_in'=&gt;$ids) );

    $tag_ids = array();
    foreach ($tags as $individual_tag)
        $tag_ids[] = $individual_tag-&gt;term_id;

    $args = array(
	'tag__in'=&gt;$tag_ids,
	'post__not_in'=&gt;$ids, // while using $recent_query we created $ids[] array with IDs of the recent posts
	'showposts'=&gt;5, // Number of related posts that will be shown.
	'caller_get_posts'=&gt;1);
    $tags_query = new WP_Query($args);
    if ($tags_query-&gt;have_posts()) {
?&gt;
    	&lt;div class=&quot;related-posts&quot;&gt;
            &lt;h2&gt;Related posts&lt;/h2&gt;
            &lt;ul&gt;

    &lt;?php while ($tags_query-&gt;have_posts()) { $tags_query-&gt;the_post(); ?&gt;           

    		&lt;li&gt;
    		    &lt;a href=&quot;&lt;?php the_permalink() ?&gt;&quot;&gt;&lt;?php the_title(); ?&gt;&lt;/a&gt;
    		&lt;/li&gt;   		

    &lt;?php } ?&gt;

            &lt;/ul&gt;
        &lt;/div&gt;

        &lt;?php
      }
?&gt;
</pre><p>The first we have create an array of tags and exclude recent posts. This we do by adding an array $ids to the parameter &#8216;post__not_in&#8217;.</p><pre class="brush: php; title: ; notranslate">
$tags = wp_get_post_tags( array('showposts' =&gt; 5, 'post__not_in'=&gt;$ids) );
</pre><p>Store the tags&#8217; ID&#8217;s in the array $tag_ids</p><pre class="brush: php; title: ; notranslate">
$tag_ids = array();
    foreach ($tags as $individual_tag)
        $tag_ids[] = $individual_tag-&gt;term_id;
</pre><p>Prepare $args &#8211; arguments we want to set for $tags_query instance of the WP_Query class.<br
/> Note argument &#8216;post__not_in&#8217; => $ids to which we assign array of the IDs of the recent posts. They won&#8217;t be displayed in the related posts.</p><pre class="brush: php; title: ; notranslate">
 $args = array(
	'tag__in'=&gt;$tag_ids,
	'post__not_in'=&gt;$ids, // while using $recent_query we created $ids[] array with IDs of the recent posts
	'showposts'=&gt;5, // Number of related posts that will be shown.
	'caller_get_posts'=&gt;1);
    $tags_query = new WP_Query($args);
</pre><p>If there are posts that meet our conditions:</p><pre class="brush: php; title: ; notranslate">
if ($tags_query-&gt;have_posts()) {
</pre><p>Start displaying related posts based on the their tags:</p><pre class="brush: php; html-script: true; title: ; notranslate">
&lt;div class=&quot;related-posts&quot;&gt;
            &lt;h2&gt;Related posts&lt;/h2&gt;
            &lt;ul&gt;

    &lt;?php while ($tags_query-&gt;have_posts()) { $tags_query-&gt;the_post(); ?&gt;           

    		&lt;li&gt;
    		    &lt;a href=&quot;&lt;?php the_permalink() ?&gt;&quot;&gt;&lt;?php the_title(); ?&gt;&lt;/a&gt;
    		&lt;/li&gt;   		

    &lt;?php } ?&gt;

            &lt;/ul&gt;
        &lt;/div&gt;
</pre><div
id="crp_related"><h3>Related Posts:</h3><ul><li><a
href="http://www.sramekdesign.com/wordpress/wordpress-display-posts-from-certain-category/" rel="bookmark" class="crp_title">WordPress: display posts from certain category</a><span
class="crp_excerpt"> Recently my client's design required displaying posts in sidebar only ...</span></li><li><a
href="http://www.sramekdesign.com/wordpress/display-recent-comments-with-avatar/" rel="bookmark" class="crp_title">Display recent comments with avatar</a><span
class="crp_excerpt"> From time to time clients ask me implement recent posts. ...</span></li><li><a
href="http://www.sramekdesign.com/wordpress/display-last-posts/" rel="bookmark" class="crp_title">Display last posts</a><span
class="crp_excerpt"> If you want to display five last posts from your ...</span></li><li><a
href="http://www.sramekdesign.com/wordpress/plugins/youtube-video-box-plugin/" rel="bookmark" class="crp_title">Youtube Video Box Plugin</a><span
class="crp_excerpt"> With Youtube Videobox plugin you can add unlimited number of ...</span></li></ul></div>]]></content:encoded> <wfw:commentRss>http://www.sramekdesign.com/wordpress/display-recent-and-related-posts-without-duplicating-each-other/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> </channel> </rss>
<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk: basic
Page Caching using disk: basic (User agent is rejected)
Database Caching 2/24 queries in 0.034 seconds using disk: basic
Object Caching 328/377 objects using disk: basic
Content Delivery Network via N/A

Served from: www.sramekdesign.com @ 2012-02-04 04:25:57 -->
