Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Is there a way to fetch the URLs of ALL images in the media gallery?

I think this would be an easy way for a website to have a Pictures page that just pulls all of the images from the media gallery, granted it would only be necessary in certain scenarios.

I don't need instructions on how to create a Pictures page, just how to pull all of the image URLs. Thanks!

share|improve this question
    
Do you mean all images in the entire Media library (i.e., site-wide)? –  ZaMoose Mar 12 '11 at 15:44
    
@ZaMoose - Yes, exactly. :) –  Jared Mar 12 '11 at 20:53
add comment

5 Answers

up vote 19 down vote accepted
+50
$query_images_args = array(
    'post_type' => 'attachment', 'post_mime_type' =>'image', 'post_status' => 'inherit', 'posts_per_page' => -1,
);

$query_images = new WP_Query( $query_images_args );
$images = array();
foreach ( $query_images->posts as $image) {
    $images[]= wp_get_attachment_url( $image->ID );
}

All the images url are now in $images;

share|improve this answer
    
um.. looks like @somatic has beat me to it. Unlike his solution above, mine will only get images. –  Azizur Mar 12 '11 at 22:19
2  
obviously our methods are similar... and azizur is right, adding the 'post_mime_type' to either query will narrow the types returned. one thing to consider: the guid often does contain the full url to the image, but it is not a reliable source. It is static, generated only once when the post is created, and is built on the current site url and the media folder structure. But that folder structure and the domain could change at some point, and then the guid is not the actual image URL anymore, just a record of what it was when it was created... –  somatic Mar 13 '11 at 3:43
    
This answer is WRONG. It does not get images from Media Library. It gets images used inside posts. Unused images are not found! –  Christian Oct 10 '11 at 12:38
    
@Christian - is it wrong? Or should I ask 'still' wrong? I realise I'm commenting almost 2 years later, but I tried this out on WP 3.6 and I'm receiving images that I've just added to the media library without adding them to any posts :/ –  Chris Kempen Aug 16 '13 at 17:19
add comment
$media_query = new WP_Query(
    array(
        'post_type' => 'attachment',
        'post_status' => 'inherit',
        'posts_per_page' => -1,
    )
);
$list = array();
foreach ($media_query->posts as $post) {
    $list[] = wp_get_attachment_url($post->ID);
}
// do something with $list here;

Query the db for all media library items (not just ones attached to posts), grab their url, dump them all in $list array.

share|improve this answer
add comment
<?php
    $attachments = get_children( array('post_parent' => get_the_ID(), 'post_type' => 'attachment', 'post_mime_type' =>'image') );
    foreach ( $attachments as $attachment_id => $attachment ) {
            echo wp_get_attachment_image( $attachment_id, 'medium' );
    }
?>

This pulls all attachments for a post/page. Attach more images to a post, and it will be listed

share|improve this answer
add comment

ok y used this code for show ALL images in media Library!

$args = array(
    'post_type' => 'attachment',
    'post_status' => 'published',
    'posts_per_page' =>25,
    'post_parent' => 210, // Post-> ID;
    'numberposts' => null,
);

$attachments = get_posts($args);

$post_count = count ($attachments);

if ($attachments) {
    foreach ($attachments as $attachment) {
    echo "<div class=\"post photo col3\">";
        $url = get_attachment_link($attachment->ID);// extraigo la _posturl del attachmnet      
        $img = wp_get_attachment_url($attachment->ID);
        $title = get_the_title($attachment->post_parent);//extraigo titulo
        echo '<a href="'.$url.'"><img title="'.$title.'" src="'.get_bloginfo('template_url').'/timthumb.php?src='.$img.'&w=350&h=500&zc=3"></a>';
        echo "</div>";
    }   
}

and if you know method for show pagination, please answer.

share|improve this answer
add comment

It looks as though it hasn't been updated in a while, but the Media Library Gallery plugin might be a good example to start looking at.

share|improve this answer
    
I just tested this plugin and it works on WordPress 3.3.2 as well. –  Sam Apr 23 '12 at 14:08
add comment

protected by toscho Jun 8 '12 at 11:46

Thank you for your interest in this question. Because it has attracted low-quality answers, posting an answer now requires 10 reputation on this site.

Would you like to answer one of these unanswered questions instead?

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