Posted on November 24, 2011, at 9:40 am
You might find a need in WordPress to get an attachment from its source URL. I use it to ID images, I haven’t tried it with anything else. Obviously, this only works if you’ve uploaded the attachment through the WordPress interface.
//gets the attachment id by giving it the source URL of the image or other attachment.
function get_attachment_id_from_src ($attachment_src) {
global $wpdb;
$query = "SELECT ID FROM {$wpdb->posts} WHERE guid='$attachment_src'";
$id = $wpdb->get_var($query);
return $id;
}
