How to get image attachments from multiple (specified) posts

I’m looking for a method to pull thumbnails as well as the source for the large image (to be used as a mini gallery) that pulls image attachments from multiple specified post ID’s.

Basically a query for post attachments that combines all the images into one mini gallery.

Read More

I’ve seen how to pull image attachments from a single post, but no idea how to pull image attachments from multiple posts?

In other words, can we pull image attachments from a specified array of posts?

Below is the code i’m using to pull ALL attachments throughout the site:

<?php
$args = array( 'post_type' => 'attachment', 'numberposts' => -1, 'post_status' => null, 'post_parent' => null ); 
$attachments = get_posts( $args );
if ($attachments) {
    foreach ( $attachments as $post ) {
        setup_postdata($post);
        the_title();
        the_attachment_link($post->ID, false);
        the_excerpt();
    }
}
?>

Related posts

Leave a Reply

1 comment

  1. After much searching I had to conclude there is no elegant way to do this with just one WP_query. Instead you can call a new query for each post_id and combine the resulting attachment ids into one array. Use this array for your further purposes.