For various reasons, I can't use Wordpress' built-in Thumbnail functionality.

What I want to do instead is to use the first image in the post as the thumbnail.

Here's what I found in the codex: Show the first image associated with the post.

However, the problem with that is that if the post has multiple images, but the first image in the post is not actually the one that was uploaded first, you'll end up getting the second image instead of the first one.

So, I decided to use something similar to this approach, which uses a regex to parse the_content to find the first post.

This works out fine, but I end up with the image size that was used in the post, and I only want the Thumbnail size.

So, here's the question: If I have a link to an image, is there any way I can get a different size?

Seems what I need is to somehow get the attachment ID, so that I can get the image size with this:

wp_get_attachment_link( $id, 'thumbnail' );

The problem is, how do I get an ID if all I have is the URL?

3 Answers 3


I decided to use this, which is based on @AndresYanez's answer:

function get_image_id_by_link($link)
    global $wpdb;

    $link = preg_replace('/-\d+x\d+(?=\.(jpg|jpeg|png|gif)$)/i', '', $link);

    return $wpdb->get_var("SELECT ID FROM {$wpdb->posts} WHERE BINARY guid='$link'");

This is much more succinct (since it doesn't jump through the hoops of first removing the extension and then adding it back in), and is a little more accurate (since the . is escaped and the query is case sensitive).

function get_attachment_id_from_src ($src) {
    global $wpdb;

    $reg = "/-[0-9]+x[0-9]+?.(jpg|jpeg|png|gif)$/i";

    $src1 = preg_replace($reg,'',$src);

    if($src1 != $src){
        $ext = pathinfo($src, PATHINFO_EXTENSION);
        $src = $src1 . '.' .$ext;

    $query = "SELECT ID FROM {$wpdb->posts} WHERE guid='$src'";
    $id = $wpdb->get_var($query);

    return $id;

Credits to pathorsley: http://www.pathorsley.com/code/get-the-wordpress-post-attachment-id-from-an-image-src/

  • +1 Thanks for your help. You got me thinking in the right direction. However, this is way too verbose, and is not completely accurate. I modified your idea slightly, and posted it as an answer. Thanks. Jan 29, 2012 at 20:09

The Codex is a valid source

Sometimes The Codex isn't that wrong...

Show attachments for the current post

This is a slightly modified example from the Codex.

// Do this inside The Loop (where $post->ID is available).
global $post;
$args = array( 
     'post_type'    => 'attachment'
    ,'numberposts'  => 1
    ,'post_status'  => null
    ,'post_parent'  => $post->ID
    ,'orderby'      => 'ID'
    ,'order'        => 'ASC' 
$attachments = get_posts( $args );
if ( $attachments ) 
    foreach ( $attachments as $attachment ) 
        echo apply_filters( 'the_title' , $attachment->post_title );
        the_attachment_link( $attachment->ID , false );

Be smart - use the system behind the system

The changes to the Codex example are easy: The numberposts are set to 1, the orderby value is the ID and it's sorting ASC to get the post with the lowest ID first.

So here's why this is smart: IDs are given one after each other, so the first uploaded post will have the lowest ID.

In the example above, you can simply exchange the last function with wp_get_attachment_link() and save it in some var, that you can reuse later.

  • As pointed out above, the problem with this method is that it'll give you the first picture uploaded - not the first picture in the post. So, if you have a picture in your post, then decide to add another one higher up in the post than the other image, your code will return the second image instead of the first one. Jan 19, 2012 at 4:55

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.