WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a Projects custom post type with a custom taxonomy of Services. Each project can have multiple services ticked. On the project page I am using the following code to pull back related projects

//for in the loop, display all "content", regardless of post_type,
//that have the same custom taxonomy (e.g. genre) terms as the current post
$backup = $post;  // backup the current object
$found_none = '';
$taxonomy = 'services';//  e.g. post_tag, category, custom taxonomy
$param_type = 'services'; //  e.g. tag__in, category__in, but genre__in will NOT work
$post_types = get_post_types( array('public' => true), 'names' );
$tax_args=array('orderby' => 'none');
$tags = wp_get_post_terms( $post->ID , $taxonomy, $tax_args);

if ($tags) {
echo "<section class='divide'>";
echo "<div class='container'>";
echo "<h2 class='no-border'>Related projects</h2>";
echo "<ul class='portfolio'>";
  foreach ($tags as $tag) {
      "$param_type" => $tag->slug,
      'post__not_in' => array($post->ID),
      'post_type' => $post_types,
    $my_query = null;
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
      <?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
          <a href="<?php the_permalink(); ?>">
            <img src="<?php $src = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), array( 315,225 ), true, '' ); echo $src[0]; ?>" />
            <h3><?php the_title(); ?></h3>
            <?php the_field('client'); ?>

        <?php $found_none = '';
      endwhile; ?>

  echo '</ul>';
  echo "</div>";
  echo "</section>";
if ($found_none) {
echo $found_none;
$post = $backup;  // copy it back
wp_reset_query(); // to use the original query again

Initially it looked like it was working fine but on closer inspection if a project has more than one service then it is duplicating the posts that are pulled back.

So any tips? Is there a better way to pull back related posts (by taxonomy) and how do I stop the duplication of results?


share|improve this question

You're running a separate query for every tag, which aside from being wasteful is also causing the repeat posts. You should rewrite the code to make only one query that looks for all tags. Try something along the lines of this:


// Generate an array of taxonomy IDs
$tax_IDs = array();
foreach ($tags as $tag) {
    $tax_IDs[] = $tag->ID;

// Use your array of taxonomy IDs in the query args
$args = array(
  'post_type' => 'your_custom_post_type',
  'post__not_in' => array($post->ID),
  'showposts'=> 3,
  'tax_query' => array(
            'taxonomy' => 'your_custom_taxonomy',
            'field' => 'id',
            'terms' => $tax_IDs

// Run your query
$my_query = new WP_Query($args);

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.