is possible to get images added to a post programmatically? I am working on custom template (my first) and I need to display Images added to a post in specific way.(first image as title image and the rest of imgs only render into hidden img tags (will be visible through lightbox slideshow).

So is there any function like get_post_attachments('type'=>'image') whose output I would be able to iterate over a loop?

Thanks for your help

3 Answers 3


You can use get_posts() (Codex ref for getting Post attachments).

$args = array( 
    'post_type' => 'attachment', 
    'post_mime_type' => 'image',
    'numberposts' => -1, 
    'post_status' => null, 
    'post_parent' => $post->ID 
$attached_images = get_posts( $args );
  • the two of us are very synchronized today :)
    – mike23
    Commented Jul 14, 2011 at 16:11

This gets all the images attached to a post :

$args = array( 'post_type' => 'attachment', 'numberposts' => -1, 'post_status' => null, 'post_mime_type' => 'image', 'post_parent' => $post->ID ); 
$attachments = get_posts( $args );
if ( $attachments ) {
    foreach ( $attachments as $attachment ) {
    ...do stuff

Have a ’look at wp_get_attachment_image()’ as well as the related functions, it'll get you started.

  • Uncanny, really. :) Commented Jul 14, 2011 at 16:17

Please note that post_parent field DOES NOT register an image when it's not being uploaded and added to a post at the same time.

This means that any image that is already loaded to library, will have a 0 (zero) value on the post_parent when it's attached to a post.

This is not useful,. Only way to get ALL the attached images to a post is parse the html and retrieve <img> tags of content.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.