I'm trying to create an image gallery which is comprised of all of the images attached to posts. The query below works well, but it ends up with all attached images rather than just the images actually used in the published post and post gallery. Unfortunately on some posts there are 20 attachments with only 10-15 of them being used within the actual post.
Is there a way to modify this query which will return only the images which have been used within the posts and post galleries?
// Get All Post IDs $post_image_query = new WP_Query( array( 'post_type' => 'post', 'posts_per_page' => -1, 'fields' => 'ids' ) ); // Get All Attachments of Posts from $post_image_query $the_query = new WP_Query( array( 'post_type' => 'attachment', 'post_status' => 'inherit', 'post_parent__in' => $post_image_query->posts ); );