<?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; WP_Query</title> <atom:link href="http://www.sramekdesign.com/tag/wp_query/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>Tue, 30 Mar 2010 08:22:59 +0000</lastBuildDate> <generator>http://wordpress.org/?v=2.9.2</generator> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <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 posts must be stored [...]]]></description> <content:encoded><![CDATA[<div
class="tweetmeme_button" style="float: right; margin: 0 0 10px 10px;"> <a
href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.sramekdesign.com%2Fwordpress%2Fdisplay-recent-and-related-posts-without-duplicating-each-other%2F"><br
/> <img
src="http://api.tweetmeme.com/imagebutton.gif?url=?url=http%3A%2F%2Fwww.sramekdesign.com%2Fwordpress%2Fdisplay-recent-and-related-posts-without-duplicating-each-other%2F&amp;source=TomSramek&amp;style=normal&amp;hashtags=do+not+duplicate,function,recent+posts,related+posts,WP_Query" height="61" width="51" /><br
/> </a></div><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;">
&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;">
&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;">
&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;">
&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;">
&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;">
$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;">
$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;">
 $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;">
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;">
&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>]]></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> <item><title>Wordpress: display posts from certain category</title><link>http://www.sramekdesign.com/wordpress/wordpress-display-posts-from-certain-category/</link> <comments>http://www.sramekdesign.com/wordpress/wordpress-display-posts-from-certain-category/#comments</comments> <pubDate>Sun, 08 Mar 2009 09:46:06 +0000</pubDate> <dc:creator>Tomas Sramek</dc:creator> <category><![CDATA[Wordpress]]></category> <category><![CDATA[category]]></category> <category><![CDATA[loop]]></category> <category><![CDATA[query_posts]]></category> <category><![CDATA[show]]></category> <category><![CDATA[showposts]]></category> <category><![CDATA[WP_Query]]></category><guid
isPermaLink="false">http://www.sramekdesign.com/?p=185</guid> <description><![CDATA[
Recently my client&#8217;s design required displaying posts in sidebar only from one category. To solve this problem I decided to use class WP_Query.
The first we have to create instance of WP_Query:Then call method query(); to start the query. This is actually same as you would be using query_posts();
Parameter cat indicates which category&#8217;s posts are shown. [...]]]></description> <content:encoded><![CDATA[<div
class="tweetmeme_button" style="float: right; margin: 0 0 10px 10px;"> <a
href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.sramekdesign.com%2Fwordpress%2Fwordpress-display-posts-from-certain-category%2F"><br
/> <img
src="http://api.tweetmeme.com/imagebutton.gif?url=?url=http%3A%2F%2Fwww.sramekdesign.com%2Fwordpress%2Fwordpress-display-posts-from-certain-category%2F&amp;source=TomSramek&amp;style=normal&amp;hashtags=category,loop,query_posts,show,showposts,Wordpress,WP_Query" height="61" width="51" /><br
/> </a></div><p>Recently my client&#8217;s design required displaying posts in sidebar only from one category. To solve this problem I decided to use class WP_Query.</p><p>The first we have to create instance of WP_Query:</p><pre lang="php"><?php $recent = new WP_Query(); ?></pre><p>Then call method query(); to start the query. This is actually same as you would be using query_posts();<br
/> Parameter <strong>cat</strong> indicates which category&#8217;s posts are shown. Parameter <strong>showposts</strong> is the number of posts to be displayed.</p><pre lang="php"><?php $recent->query('cat=1&#038;showposts=5'); ?></pre><p>Now we can use loop:</p><pre class="brush: php;">
&lt;?php while($recent-&gt;have_posts()) : $recent-&gt;the_post(); ?&gt;
&lt;!-- Here goes some code --&gt;
&lt;?php endwhile; ?&gt;
</pre><p>Final code that displays last 5 posts&#8217; permalinks from category with ID=1 looks like this:</p><pre class="brush: php;">
&lt;h2&gt;Last posts&lt;/h2&gt;
&lt;?php $recent = new WP_Query(); ?&gt;
&lt;?php $recent-&gt;query('cat=1&amp;showposts=5'); ?&gt;
&lt;?php while($recent-&gt;have_posts()) : $recent-&gt;the_post(); ?&gt;
&lt;ul&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;/ul&gt;
&lt;?php endwhile; ?&gt;
</pre><p>Using your own WP_Query instance and loop prevents you from problems that might cause plugins&#8217; loops and use of conditional tags.</p> ]]></content:encoded> <wfw:commentRss>http://www.sramekdesign.com/wordpress/wordpress-display-posts-from-certain-category/feed/</wfw:commentRss> <slash:comments>15</slash:comments> </item> </channel> </rss>
<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk
Page Caching using disk (user agent is rejected)
Database Caching 10/15 queries in 0.005 seconds using disk

Served from: www.sramekdesign.com @ 2010-08-01 07:31:26 -->