Dec
3
2013
This will take the url of any image size and return the id of the attachment
1 2 3 4 5 6 7 8 9 10 11 12 |
function get_attachment_id_from_src($image_url) { global $wpdb, $table_prefix; $res = $wpdb->get_results('select post_id from ' . $table_prefix . 'postmeta where meta_value like "%' . basename($image_url). '%"'); if(count($res) == 0) { $res = $wpdb->get_results('select ID as post_id from ' . $table_prefix . 'posts where guid="' . $image_url . '"'); } return $res[0]->post_id; } |