0

I'm trying to display related posts by a custom taxonomy (product_tags) in a custom post type (products). The following code works, but it ignores posts_per_page.

Any ideas?

                global $post;
                $terms = get_the_terms( $post->ID , 'product_tags', 'string');
                $do_not_duplicate[] = $post->ID;

                if(!empty($terms)){
                    echo '<strong>Related Products</strong>';
                    foreach ($terms as $term) {
                        query_posts( array(
                        'post_type' => 'products',
                        'product_tags' => $term->slug,
                        'posts_per_page' => 3,
                        'ignore_sticky_posts' => 1,
                        'orderby' => 'rand',
                        'post__not_in' => $do_not_duplicate) );
                        if(have_posts()) {
                            while ( have_posts() ) : the_post(); $do_not_duplicate[] = $post->ID; ?>
                            <div class="single_related">
                            <?php if (has_post_thumbnail()) { ?>
                                <a href="<?php the_permalink() ?>" title="<?php the_title(); ?>">
                                    <?php the_post_thumbnail( 'related_sm', array('alt' => get_the_title()) ); ?>
                                </a>
                            <?php } else { ?>
                                <a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a>
                            <?php } ?>
                            </div>
                            <?php endwhile; wp_reset_query();
                        }
                    }
                }
1
  • Ignores post_per_page? I take it you want 3 posts in total, rather than three posts for each term of the current post? Feb 23, 2012 at 23:47

2 Answers 2

5

If you want to query for multiple taxonomy terms you can use the more advanced, tax_query argument of WP_Query queries. For instance

//Get array of terms
$terms = get_the_terms( $post->ID , 'product_tags', 'string');
//Pluck out the IDs to get an array of IDS
$term_ids = wp_list_pluck($terms,'term_id');

//Query posts with tax_query. Choose in 'IN' if want to query posts with any of the terms
//Chose 'AND' if you want to query for posts with all terms
  $second_query = new WP_Query( array(
      'post_type' => 'products',
      'tax_query' => array(
                    array(
                        'taxonomy' => 'product_tags',
                        'field' => 'id',
                        'terms' => $term_ids,
                        'operator'=> 'IN' //Or 'AND' or 'NOT IN'
                     )),
      'posts_per_page' => 3,
      'ignore_sticky_posts' => 1,
      'orderby' => 'rand',
      'post__not_in'=>array($post->ID)
   ) );

//Loop through posts and display...
    if($second_query->have_posts()) {
     while ($second_query->have_posts() ) : $second_query->the_post(); ?>
      <div class="single_related">
           <?php if (has_post_thumbnail()) { ?>
            <a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"> <?php the_post_thumbnail( 'related_sm', array('alt' => get_the_title()) ); ?> </a>
            <?php } else { ?>
                 <a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a>
            <?php } ?>
       </div>
   <?php endwhile; wp_reset_query();
   }

Note: This seems like it might be a 'secondary loop', i.e. not the 'main' Loop (where posts / a single post are displayed). If so, you should not being use query_posts. See this excellent question.

5
  • You're right, this is a secondary loop. I'm not sure what's going on with this code, but when I replace my old code with this, the page won't even load. No errors or anything... just a blank page. Feb 24, 2012 at 0:11
  • Ah, found the error in that. It's missing a comma after 'terms' => $term_ids. So anyway, this is is still not respecting the posts_per_page AND now it's also showing the post of the page we're on. Feb 24, 2012 at 0:29
  • What do you mean by not respecting the posts per page? Feb 24, 2012 at 0:31
  • I mean it's ignoring it. Feb 24, 2012 at 0:33
  • Edited to exclude current post and fixed the bug. Feb 24, 2012 at 0:34
0

This probably isn't the correct solution, but I would start by moving $term->slug:

foreach ($terms as $term) {
  $termSlug = $term->slug;
  query_posts( array(
    'post_type' => 'products',
    'product_tags' => $termSlug,
    'posts_per_page' => 3,
    ...
3
  • That didn't seem to do anything. Feb 23, 2012 at 23:48
  • sorry, it was worth a shot... fixed the formatting anyway Feb 24, 2012 at 0:02
  • did you do a print_r on $term and make sure everything looks right? Feb 24, 2012 at 0:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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