WebDev
Category: Wordpress
Get Attachment ID from Source URL
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;
}

More Updates...
  • Postnatal care in France. Interesting!
  • The Snuggle Sutra For Valentine’s day.
  • Movie Posters re-imagined in another time.
  • Pepper's too pushy! Nominating a new consort for Salt…
  • Was Marilyn Monroe fat? Simon Doonan reveals what he discovered when rifling through the icon’s belongings.
  • Bill Hicks would have turned 50 recently.
  • Ross Douthat, Gravedigger
  • Slate's Announcement of Christopher Hitchens’ passing.
  • See more...
    Blair Kelly
    I'm a skeptic, freethinker, and secular humanist.

    After a year as a content developer at a successful IT company, I'm taking some time to regroup and set a new course.

    I hold a combined honours degree in communications studies and multimedia from McMaster University.