Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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();
                        }
                    }
                }
share|improve this question
    
Ignores post_per_page? I take it you want 3 posts in total, rather than three posts for each term of the current post? –  Stephen Harris Feb 23 '12 at 23:47
add comment

2 Answers

up vote 2 down vote accepted

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.

share|improve this answer
    
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. –  Jake Caputo Feb 24 '12 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. –  Jake Caputo Feb 24 '12 at 0:29
    
What do you mean by not respecting the posts per page? –  Stephen Harris Feb 24 '12 at 0:31
    
I mean it's ignoring it. –  Jake Caputo Feb 24 '12 at 0:33
    
Edited to exclude current post and fixed the bug. –  Stephen Harris Feb 24 '12 at 0:34
show 2 more comments

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,
    ...
share|improve this answer
    
That didn't seem to do anything. –  Jake Caputo Feb 23 '12 at 23:48
    
sorry, it was worth a shot... fixed the formatting anyway –  Zach L Feb 24 '12 at 0:02
    
did you do a print_r on $term and make sure everything looks right? –  Zach L Feb 24 '12 at 0:04
add comment

Your Answer

 
discard

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.