views:

25

answers:

1

Hello there, Can anyone suggest me what is the function to get all the images stored for wordpress? I just need to list all the images seen under menu Media of the wordpress admin.

Thanks in advance

+2  A: 

Uploaded images are stored as posts with the type "attachment"; use get_posts() with the right parameters. In the Codex entry for get_posts(), this example:

<?php

$args = array(
    'post_type' => 'attachment',
    'numberposts' => -1,
    'post_status' => null,
    'post_parent' => null, // any parent
    ); 
$attachments = get_posts($args);
if ($attachments) {
    foreach ($attachments as $post) {
        setup_postdata($post);
        the_title();
        the_attachment_link($post->ID, false);
        the_excerpt();
    }
}

?>

...loops through all the attachments and displays them.

If you just want to get images, you may need to filter by the mime type (I think this will be the attachment's post_mime_type attribute), but hopefully this is enough to get you headed in the right direction.

Matt Gibson
Yup, you can just use `'post_mime_type' => 'image'` in your `$args`, and WordPress will cleverly match that against all image mime types :)
TheDeadMedic
@TheDeadMedic Good to know! I've never needed to get particular types back.
Matt Gibson
Nice answer and kudos to TheDeadMedic for matching mime types! :)
hsatterwhite