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.

Basically I have regular Category Listings that include posts WITH tags and posts WITHOUT tags.

I need to modify the loop so that it ONLY includes posts which have one or more tags assigned to them.

Is there a way do this?

Thanks

share|improve this question
add comment

1 Answer

here is an example loop..
you can change the number of tag to exclude based on the amount you want...

<?php 
if (have_posts()) : while (have_posts()) : the_post();

// GET THE TAGS OF CURRENT POST IN LOOP AND COUNT THE AMOUNT
    $posttags = get_the_tags();
    $howmany = count($posttags);
    if($howmany > 2) {
?>
    <h3><?php the_title(); ?></h3>
    <?php the_excerpt(); ?>

<?php
    } else {
    // do nothing
    }
    endwhile; 
?>
<?php endif; ?>

.

Hope This Helps
Sagive

share|improve this answer
    
The Tag ID will not be known. I only want the loop to display posts which HAVE a tag set. There's over 100 tags. The posts WITH tags usually have 3 - 5. I only want them to appear in category listings. –  Klark Jun 16 '12 at 6:45
    
i see.. sorry - missread you question.. fixed the solution. –  Sagive SEO Jun 16 '12 at 7:04
    
Thanks. That does exactly what I want. The only problem is that it now doesn't show the right posts per page. For example: I have 10 posts per page, and there's 120 posts (with tags). Instead of showing just 12 paged pages with 10 posts each, it shows about 25 paged pages with varying amounts of posts per page - between 2 and 8. –  Klark Jun 16 '12 at 7:16
    
paste your current loop so i wont mass it up and i would gladly help –  Sagive SEO Jun 16 '12 at 8:59
    
Currrent loop looks like this: <?php $posts = query_posts( $query_string . '&orderby=title&order=asc&post_type=glossary' ); ?> <?php if (have_posts()) : while (have_posts()) : the_post(); // GET THE TAGS OF CURRENT POST IN LOOP AND COUNT THE AMOUNT $posttags = get_the_tags(); $howmany = count($posttags); if($howmany > 2) { ?> <div class="cat-posts"> <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3> <?php the_excerpt(); ?> </div> <?php } else { // } endwhile; ?> <?php endif; ?> –  Klark Jun 16 '12 at 14:38
show 2 more comments

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.