WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Wordpress sites this as an example of how to get the thumbnail:

<?php echo get_the_post_thumbnail( $post_id, $size, $attr ); ?>

I want to know how can I use the same basic function but just have it automatically get the latest media upload's ID number instead of having to specify the post_id.

share|improve this question
up vote 1 down vote accepted

I don't think that you can use get_the_post_thumbnail function to get last uploaded media, but you can use get_post to get latest attachment and then wp_get_attachment_image to display images.

$attachments = get_posts( array(
    'post_type' => 'attachment',
    'posts_per_page' => 1,
    'post_status' => null,
    'post_mime_type' => 'image'
) );

foreach ( $attachments as $attachment ) {
    echo wp_get_attachment_image( $attachment->ID, 'thumbnail' );
share|improve this answer
The image is unattached to anything, so I was trying to get the most recently uploaded attachment / image and then go from there... – cwd Sep 14 '11 at 13:36
@cwd This snippet doesn't work for you? – Mamaduka Sep 14 '11 at 13:45
oh, hmm, maybe it would. i guess i read your intentions for what it would do wrong. let me give it a try :) – cwd Sep 14 '11 at 13:54
The code is good, just needs one tweak echo wp_get_attachment_image( $attachment->ID, 'thumbnail' ); - needs to include the ->ID bit. thanks. – cwd Sep 19 '11 at 13:34
@cwd Thanks, edited my answer. – Mamaduka Sep 19 '11 at 17:59

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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