Get all images attached to any post

I am getting a list of all the images the following way:

$the_query = new WP_Query( array(
  'post_type'       => 'attachment',
  'post_mime_type'  => 'image',
  'post_status'     => 'inherit',
  'posts_per_page'  => -1,
 ) );

But I’m trying to get only those who are uploaded to a post on my website.

Not images attached to a specific post but exclude those who aren’t featured on any post.

Read more here: Get all images attached to any post

Leave a Reply

Your email address will not be published. Required fields are marked *