0

What I want to do seems to be simple but I can't get around it. I'd like to get the latest post that has an image attachment. A little help would be appreciated. Thanks

Lita

1
  • What I had so far was getting the latest attachment post but thank you bcorkins solution works.
    – Lita
    Commented Mar 8, 2013 at 6:43

1 Answer 1

1

This is a tricky one. If you wanted to get the latest image attachment and display the post associated with it, you could use:

$attachments = get_posts('post_type=attachment');
$first_attachment_post = get_post($attachments[0]->post_parent);

To get the latest post, but only if it has an attachment, I think the only way to do it would be with two nested queries. For example:

$the_query = new WP_Query( $args );
while ( $the_query->have_posts() ) : $the_query->the_post();

        $attachments = get_children( array('post_parent' => get_the_ID(), 'post_type' => 'attachment', 'post_mime_type' => 'image') );

        if ( $attachments ) {
            // Post output here will only be display if attachments are found
            echo '<li>' . get_the_title() . '</li>';
        }
endwhile;
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.