Especially when developing plugins or theme settings that make use of the WordPress media uploader to upload and insert images, being able to retrieve the ID of the image uploaded can be extremely useful. By grabbing the image (attachment) ID, you suddenly have a lot more control over what you can do with the image, such as display one of the different sizes that WordPress generates when the image is uploaded.
This is a quick tip. Check out more Quick Tips
This is a little function I wrote while on the train home from Chicago.
1 2 3 4 5 6 7 | // retrieves the attachment ID from the file URL function pippin_get_image_id($image_url) { global $wpdb; $prefix = $wpdb->prefix; $attachment = $wpdb->get_col($wpdb->prepare("SELECT ID FROM " . $prefix . "posts" . " WHERE guid='%s';", $image_url )); return $attachment[0]; } |
Simply pass the URL of the image ID you wish to retrieve. Once you’ve done that, you could do the following to retrieve the auto-generated thumbnail size of the image:
1 2 3 4 5 6 7 8 9 10 11 | // set the image url $image_url = 'http://yoursite.com/wp-content/uploads/2011/02/14/image_name.jpg'; // store the image ID in a var $image_id = pippin_get_image_id($image_url); // retrieve the thumbnail size of our image $image_thumb = wp_get_attachment_image_src($image_id, 'thumbnail'); // display the image echo $image_thumb[0]; |
Enjoy!

Does it work with image URLs that are not the main image? Resized versions, etc.
No, you will need the original URL, but you can then use the ID to get the other sizes.
Very useful. Great work!
Hey Pippin,
This is a rad little snippet. I rewrote it slightly to directly retrieve the desired thumb size as it can become frustrating to continually rewrite the last 3 steps.
Excellent!
I tried this out, but it seems that the “guid” is not in all cases the URL to the picture.
That is true, the guid column will not always work. There was a tutorial someone posted not too long ago with a better way to do this, but I don’t remember which site it was on unfortunately.
After combining some code from several other people who have posted about this online, here’s a function I wrote that doesn’t use GUIDs, and even works with image thumbnail URLs:
[...] no built-in function in WordPress for doing this, and a Google search provided only partial solutions, or solutions that didn’t work with thumbnail images. However, using some [...]
Looks like the comment form
tag doesn't format the text nicely. Here's my blog post with the code formatted for easier reading: http://philipnewcomer.net/2012/11/get-the-attachment-id-from-an-image-url-in-wordpress/Looks great! FYI, you can post code by wrapping it in PRE tags.
I see. The PRE tag wasn’t in the list of allowed tags below the comment box, so I used CODE.
I am using this and just upgraded to WP 3.5. Now I get this error: Warning: Missing argument 2 for wpdb::prepare(),. I found the post about how it should work now (http://make.wordpress.org/core/2012/12/12/php-warning-missing-argument-2-for-wpdb-prepare/) but am having trouble changing this code so that it is correct and not throwing the error. Can anyone help me with this?
Thanks!
I’ve updated the code to fix the error.
That works again. Thank you so much for your help!
ty for update!
Take it back, nothing has changed? Same 3.5 update problem
okay, jk it works. maybe put your example function in one block so it doesn’t look so janky? Thanks for this btw.
Whoops, fixed!
Hi Pippin,
Great tip. Using this to get the attachment ID of a logo uploaded via the WP customizer.
One quick question: Is there a reason why you use
$wpdb->prefixinstead of just using$wpdb->postsinside the SQL? Since it’s a default table should$wpdb->postsalways have the correct prefix anyway?Cheers,
Eric
Nope, no reason