<?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>mindBlog &#187; code</title>
	<atom:link href="http://www.3mind.at/category/code/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.3mind.at</link>
	<description>human after all</description>
	<lastBuildDate>Sun, 02 May 2010 19:41:22 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>WordPress: Related posts by tags</title>
		<link>http://www.3mind.at/2009/05/06/code-highlighting/</link>
		<comments>http://www.3mind.at/2009/05/06/code-highlighting/#comments</comments>
		<pubDate>Wed, 06 May 2009 08:02:44 +0000</pubDate>
		<dc:creator>3mind</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[related posts]]></category>
		<category><![CDATA[tags]]></category>
		<category><![CDATA[wordpress]]></category>

		<guid isPermaLink="false">http://www.3mind.at/?p=918</guid>
		<description><![CDATA[As I published the last two posts here on mindBlog, the similarity in the tags i used with both entries came to my attention. Therefore I thought, it would be great to somehow link related posts together by tags they have in common. I stumbled through the web and soon found a solution to my [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">As I published the last two posts here on mindBlog, the similarity in the tags i used with both entries came to my attention. Therefore I thought, it would be great to somehow link related posts together by tags they have in common. I stumbled through the web and soon found a solution to my problem. As <a title="Smashing Magazine" href="http://www.smashingmagazine.com/" target="_blank">Smashing Magazine</a> pointed out some weeks ago, there&#8217;s an easy way to show related posts based on tags with this code-snippet:</p>
<pre class="brush: php">&lt;?php
  $tags = wp_get_post_tags($post-&gt;ID);
  if ($tags) {
    echo 'Related Posts';
    $first_tag = $tags[0]-&gt;term_id;
    $args=array(
      'tag__in' =&gt; array($first_tag),
      'post__not_in' =&gt; array($post-&gt;ID),
      'showposts'=&gt;5,
      'caller_get_posts'=&gt;1
    );
    $my_query = new WP_Query($args);
    if( $my_query-&gt;have_posts() ) {
      while ($my_query-&gt;have_posts()) : $my_query-&gt;the_post(); ?&gt;
        &lt;p&gt;&lt;a href="&lt;?php the_permalink() ?&gt;" rel="bookmark" title="Permanent Link to &lt;?php the_title_attribute(); ?&gt;"&gt;&lt;?php the_title(); ?&gt;&lt;/a&gt;&lt;/p&gt;
      &lt;?php
      endwhile;
    }
  }
?&gt;</pre>
<p style="text-align: justify;">Unfortunately this only works for the first tag of a post, for example if we&#8217;d have the tags <em>2009</em>, <em>trailer</em>, and <em>movie</em>, it&#8217;ll only find all posts with <em>2009 </em><strong>as first tag</strong>! Of course this solution is unacceptable, as it limits the amount of suitable results way to much. As I found out soon, others were already complaining about this drawback, but didn&#8217;t post any solution. I don&#8217;t know why it&#8217;s even made like this, cause the WordPress-function <em>wp_get_post_tags()</em> already returns an array containing all tags we need, and therefore the solution is ridiculously  simple.</p>
<h3>Solution with the new Array</h3>
<p style="text-align: justify;">So here&#8217;s mine: the query-string on line 7 already uses an array for the <strong>tags__in</strong> parameter, so why not fill this array with all the tags the current post has. tags__in can be compared to an IN-statement used by SQL, and will therefore return all posts where one of the tags fits. The only thing we need to do is to create a new array which will hold all the tag-IDs we need (tags__in only compares tags based on their ID), and pass this array to the query-string.</p>
<pre class="brush: php">&lt;?php
  //for use in the loop, list 5 post titles related to first tag on current post
  $tags = wp_get_post_tags($post-&gt;ID);
  $tagIDs = array();
  if ($tags) {
    $tagcount = count($tags);
    for ($i = 0; $i &lt; $tagcount; $i++) {
      $tagIDs[$i] = $tags[$i]-&gt;term_id;
    }
    $args=array(
      'tag__in' =&gt; $tagIDs,
      'post__not_in' =&gt; array($post-&gt;ID),
      'showposts'=&gt;5,
      'caller_get_posts'=&gt;1
    );
    $my_query = new WP_Query($args);
    if( $my_query-&gt;have_posts() ) {
      while ($my_query-&gt;have_posts()) : $my_query-&gt;the_post(); ?&gt;
        &lt;h3&gt;&lt;a href="&lt;?php the_permalink() ?&gt;" rel="bookmark" title="&lt;?php the_title(); ?&gt;"&gt;&lt;?php the_title(); ?&gt;&lt;/a&gt;&lt;/h3&gt;
      &lt;?php endwhile;
    }
  }
?&gt;</pre>
<p>FYI: You can see the result of this code at the bottom of every article here on mindBlog which has related posts.</p>
<p><strong>Sources</strong></p>
<ul>
<li><a title="10-exceptional-wordpress-hacks" href="http://www.smashingmagazine.com/2009/04/15/10-exceptional-wordpress-hacks/" target="_blank">Smashing Magazine Article</a></li>
<li> <a title="Permanent Link to How to: Show related posts without a plugin" rel="bookmark" href="http://www.wprecipes.com/how-to-show-related-posts-without-a-plugin" target="_blank">How to: Show related posts without a plugin</a></li>
</ul>
<h3>Update</h3>
<p style="text-align: justify;">In case you&#8217;re facing problems by using the related posts and the comments on your page, here&#8217;s the solution. For some reason, WordPress struggles with the nested while-loop from the code above and the main-loop used to gather the entries for every page. To make sure everything goes smoothly, we simply backup the current $post object at the beginning of our code and copy it back at the end, where we also call the built in function wp_reset_query(). This should fix the issues some might have.</p>
<p>Here&#8217;s the complete code with the new lines:</p>
<pre class="brush: php">&lt;?php  //for use in the loop, list 5 post titles related to first tag on current post
  $backup = $post;  // backup the current object
  $tags = wp_get_post_tags($post-&gt;ID);
  $tagIDs = array();
  if ($tags) {
    $tagcount = count($tags);
    for ($i = 0; $i &lt; $tagcount; $i++) {
      $tagIDs[$i] = $tags[$i]-&gt;term_id;
    }
    $args=array(
      'tag__in' =&gt; $tagIDs,
      'post__not_in' =&gt; array($post-&gt;ID),
      'showposts'=&gt;5,
      'caller_get_posts'=&gt;1
    );
    $my_query = new WP_Query($args);
    if( $my_query-&gt;have_posts() ) {
      while ($my_query-&gt;have_posts()) : $my_query-&gt;the_post(); ?&gt;
        &lt;h3&gt;&lt;a href="&lt;?php the_permalink() ?&gt;" rel="bookmark" title="&lt;?php the_title(); ?&gt;"&gt;&lt;?php the_title(); ?&gt;&lt;/a&gt;&lt;/h3&gt;
      &lt;?php endwhile;
    } else { ?&gt;
      &lt;h2&gt;No related posts found!&lt;/h2&gt;
    &lt;?php }
  }
  $post = $backup;  // copy it back
  wp_reset_query(); // to use the original query again
?&gt;</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.3mind.at/2009/05/06/code-highlighting/feed/</wfw:commentRss>
		<slash:comments>54</slash:comments>
		</item>
		<item>
		<title>[code] Mandelbrot</title>
		<link>http://www.3mind.at/2008/12/09/code-mandelbrot/</link>
		<comments>http://www.3mind.at/2008/12/09/code-mandelbrot/#comments</comments>
		<pubDate>Tue, 09 Dec 2008 06:50:47 +0000</pubDate>
		<dc:creator>3mind</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[fun]]></category>

		<guid isPermaLink="false">http://www.3mind.at/?p=675</guid>
		<description><![CDATA[weil es eben geht &#8211; eine andere erklärung warum man folgende zeilen code produziert gibt es wohl nicht. ich hab zwar auch schon das eine oder andere TSQL-statement geschrieben, aber dass man damit auch ein Mandelbrot generieren kann ist mir neu. code output source]]></description>
			<content:encoded><![CDATA[<p>weil es eben geht &#8211; eine andere erklärung warum man folgende zeilen code produziert gibt es wohl nicht. ich hab zwar auch schon das eine oder andere TSQL-statement geschrieben, aber dass man damit auch ein Mandelbrot generieren kann ist mir neu.</p>
<p><strong>code</strong></p>
<p><a href="http://www.3mind.at/wp-content/uploads/2008/12/mandelcode.jpg" class="lightview" rel="gallery[675]" title="mandelcode"><img class="alignnone size-full wp-image-682" title="mandelcode" src="http://www.3mind.at/wp-content/uploads/2008/12/mandelcode.jpg" alt="" width="500" height="517" /></a></p>
<p><span id="more-675"></span></p>
<p><strong>output</strong></p>
<p><a href="http://www.3mind.at/wp-content/uploads/2008/12/madlebrot.png" class="lightview" rel="gallery[675]" title="madlebrot"><img class="alignnone size-full wp-image-676" title="madlebrot" src="http://www.3mind.at/wp-content/uploads/2008/12/madlebrot.png" alt="" width="489" height="764" /></a></p>
<p><a href="http://thedailywtf.com/Articles/Stupid-Coding-Tricks-The-TSQL-Madlebrot.aspx" target="_blank">source</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.3mind.at/2008/12/09/code-mandelbrot/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
