Show only, when taxonomy has posts?

I’d like to query posts by taxonomy like this:

<?php
global $post;
$terms = get_the_terms( $post->ID , 'movies', 'string');
$do_not_duplicate[] = $post->ID;

if(!empty($terms)){
    foreach ($terms as $term) {
        query_posts( array(
        'movies' => $term->slug,
        'showposts' => 4,
        'caller_get_posts' => 1,
        'post__not_in' => $do_not_duplicate ) );
        if(have_posts()){
            while ( have_posts() ) : the_post(); $do_not_duplicate[] = $post->ID; ?>
do stuff 

            <?php endwhile; wp_reset_query();
        }
    }
}
?>

But I only want to show this, while there are posts with the same taxonomy to show up.

Read More

How could I do this?

Something like: if has taxonomy AND has post?

Thanks!

AD

Related posts

Leave a Reply

1 comment

  1. If you take a look at the array that get_the_terms() returns, you’ll see that it includes a “count”.

    If I understand your question correctly, you’re looking to find all posts with a count greater than 1 (i.e. terms that are on more than the current post). So, I think you can simply change your foreach statement to this (the second line is the change):

    foreach ($terms as $term) {
      if( $term->count > 1 ) {
        query_posts( array(
        'movies' => $term->slug,
        'showposts' => 4,
        'caller_get_posts' => 1,
        'post__not_in' => $do_not_duplicate ) );
        if(have_posts()){
            while ( have_posts() ) : the_post(); $do_not_duplicate[] = $post->ID; ?>
                  // do stuff 
            <?php endwhile; wp_reset_query();
        }
      }
    }
    

    Also, get_the_terms() only accepts two parameters (at least according to the Codex), so I’m not sure on what that third parameter is doing. It’s possibly not doing anything? If you’re trying to get terms from two taxonomies, check out wp_get_object_terms()