Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

240
Views
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.

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

All you have to do is paste the following code inside a loop.

$args = array(
        'post_parent'    => get_the_ID(), // your post id
        'post_type'      => 'attachment',
        'numberposts'    => -1, // show all
        'post_status'    => 'any',
        'post_mime_type' => 'image',
        'orderby'        => 'menu_order',
        'order'           => 'ASC'
   );

$images = get_posts($args);
if($images) { ?>
    <img src="<?php echo wp_get_attachment_url($image->ID); ?>" />
<?php
}
?>
over 4 years ago · Santiago Trujillo Report

0

I think the post_mime_type is not correct.

$args = array(
    'post_type'  => 'attachment',
    'post_status'    => 'inherit',
    'post_mime_type' => 'image/gif',
);

$query = new WP_Query( $args );

Hope this helps!

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!