title: How to create related posts functionality for wordpress custom post type posts when one posts belongs to many categories
body: <p>I need to add related posts functionality in the single page of wordpress custom post type.I have posts that belong to multiple categories and that posts are shown twice one at the top when displaying its content and also itself is shown in the related posts part.I tried with this code </p>
  
  <pre><code>&lt;?php
          $backup = $post;  // backup the current object
          $found_none = '&lt;h2&gt;No related posts found!&lt;/h2&gt;';
          $taxonomy = 'news-category';//  e.g. post_tag, category, custom taxonomy
          $param_type = 'news-category'; //  e.g. tag__in, category__in, but genre__in will NOT work
          $post_types = get_post_types( array('public' =&gt; true), 'names' );
          $tax_args=array('orderby' =&gt; 'none');
          $tags = wp_get_post_terms( $post-&gt;ID , $taxonomy, $tax_args);
          if ($tags) {
            foreach ($tags as $tag) {
              $args=array(
                "$param_type" =&gt; $tag-&gt;slug,
                'post__not_in' =&gt; array($post-&gt;ID),
                'post_type' =&gt; $post_types,
                'showposts'=&gt;-1,
                'caller_get_posts'=&gt;1
              );
              $my_query = null;
              $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;div class="related_post_item"&gt;
                  &lt;h2&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;/h2&gt;
                  &lt;div class="news_date"&gt;&lt;?php echo get_the_date('F Y', $post-&gt;ID); ?&gt;&lt;/div&gt;
                  &lt;?php $found_none = ''; ?&gt;
                                  &lt;/div&gt;
              &lt;?php  endwhile;
              }
            }
          }
          if ($found_none) {
          echo $found_none;
          }
          $post = $backup;  // copy it back
          wp_reset_query(); // to use the original query again
          ?&gt;
  </code></pre>
  
  <p>It works perfectly when one category is checked per post, but I have posts that belong to multiple categories.How to detect related posts correctly when having multiple categories checked?</p>