Exclude Posts Without Attachments From Query
-
What I’m trying to do is generate a loop that only displays posts with image attachments. I’m able to hide posts without attachments but they’re still counted in the loop, so if I have 5 posts and 1 does not have an attachment only 4 posts are displayed. I want to always be able to display 5 posts, so the next post to have an attachment becomes post number 5.
This is the code I am using:
<?php query_posts('posts_per_page=5&meta_key=slider&meta_value=Include'); if (have_posts()) : while (have_posts()) : the_post(); ?> <?php $args = array( 'post_type' => 'attachment', 'numberposts' => 1, 'post_status' => null, 'post_mime_type' => 'image', 'orderby' => menu_order, 'order' => ASC, 'post_parent' => $post->ID ); $attachments = get_posts($args); if ($attachments) { foreach ($attachments as $attachment) { ?> <div> <span class="slider-text"> <h3><a href="<?php the_permalink(); ?>" rel="bookmark" title="<?php the_title(); ?>"><?php echo slidertitle(get_the_title());?></a></h3> </span> <div class="slider-image"> <img src="<?php echo wp_get_attachment_url($attachment->ID); ?>" alt="" /> </div> </div> <?php } ?> <?php endwhile; endif; wp_reset_query(); ?>
I would like to able to add a post_type=attachment criteria to query_posts but this does not work.
Viewing 12 replies - 1 through 12 (of 12 total)
Viewing 12 replies - 1 through 12 (of 12 total)
- The topic ‘Exclude Posts Without Attachments From Query’ is closed to new replies.