$posts = get_posts( array( 'post_type' => 'post', 'posts_per_page' => -1 ) );
foreach ( $posts as $post ) {
$attachments = get_children( array (
'post_parent' => $post->ID,
'post_type' => 'attachment',
'post_mime_type' => 'image',
'post_per_page' => 25
), ARRAY_A );
if( $attachments ) {
//continue with what you were doing
}
}