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'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.

How could I do this?

Something like: if has taxonomy AND has post?

Thanks!

AD

share|improve this question
    
Do mean posts other than the current one? get_the_terms will return terms that belong to this post - and so it follows each term has at least one post associated to it :) –  Stephen Harris Jul 3 '12 at 0:03
    
aha....ok...I will try something else :) –  ad2003 Jul 3 '12 at 13:17
add comment

1 Answer 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()

share|improve this answer
    
dear mrwweb thanks for the reply. It's kind of a related post on taxonomies. So when theres a post about "Pulp Fiction" with a slug e.g. "pulp-fiction" and another somewhere around, this should be featured. If no other post has this slug, the related posts shouldn't be shown. I tried it, but nothing is showing up... I put your code into mine, but it doesn't work. –  ad2003 Jul 3 '12 at 7:26
    
If this is a related posts loop, then you should be using get_posts() or WP_Query (I prefer the latter). With query_posts() you may be getting arguments that you don't want. If you go with WP_Query, then you'll need to use a tax_query instead of 'movies' => $term->slug, but the documentation on the codex is easy to follow. What showed up before you changed the code? –  mrwweb Jul 3 '12 at 14:10
    
thanks! I will give it a try! AD –  ad2003 Jul 3 '12 at 23:12
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.