Is there any way I can take a URL of an image and find the attachment or post id of that image in the database?
Here is the situation:
I’m in a loop going over all the ‘img’ tags that are surrounded by ‘a’ tags in my post content. if the src attribute of the ‘img’ tag does not match the href attribute of the outer ‘a’ tag, then i want to replace the ‘img’ tag. In doing this, if the ‘img’ that is to be removed is in the gallery, i want to delete that post, and then put my replacement ‘img’ in its place. I tried using a function like this:
function find_image_post_id($url) {
global $wpdb;
$postid = $wpdb->get_var($wpdb->prepare("SELECT DISTINCT ID FROM $wpdb->posts WHERE guid='$url'"));
if ($postid) {
return $postid;
}
return false;
}
This apparently is not right because the guid is ironically not globally unique. I had (earlier in the same script) uploaded a file with the same name (why? because it was higher resolution and i am trying to replace low resolution versions of the same image) and although wordpress will save the image with a different name in the directory, the guid’s were set to be the same. (possibly a bug).
Is there another technique I can use?
Massively improved function developed for plugin heavy on images:
All those complex functions can be reduced to one simple function:
attachment_url_to_postid()
You only need to parse the image URL to retrieve the attachment ID:
That’s all you need.
I modified Rarst’s code to allow you to match just the filename instead of the full path. This is helpful if you are about to sideload the image if it does not exist. Currently this only works if file names are unique but I will be adding a hash check later to help with images that have the same filename.
Ok I found the answer that no one has on the net I been looking for days now. Keep in mine this only works if your theme or plugin is using the
WP_Customize_Image_Control()
if you are usingWP_Customize_Media_Control()
theget_theme_mod()
will return the ID and not the url.For my solution I was using the newer version
WP_Customize_Image_Control()
A lot of posts on the forums have the
get_attachment_id()
which does not work anymore. I usedattachment_url_to_postid()
Here is how I was able to do it. Hope this helps someone out there
Markup
Since wordpress version 4.0 there is a wordpress core function attachment_url_to_postid that “Tries to convert an attachment URL into a post ID.” But these function has huge flaw cos it can’t convert custom image url sizes.
Almost all top answers before trying solve these problem, but all of them have a very heavy database query trying to get all existing attachments and then loop through all of them trying to get attachment metadata and find custom sizes in it. It works well only if you have small amount of attachments in your wordpress project. But you definitely will get performance problem if you have big project with 10000 + attachments.
But you can extend attachment_url_to_postid core function through existing filter attachment_url_to_postid then trying to force attachment_url_to_postid function search among custom image sizes too.
Just add code below to functions.php file of your active wordpress theme.
and then use attachment_url_to_postid function as you use before but with additional custom sizes functionality.
Here is an alternative solution:
Since WP 4.0 they introduced a function
attachment_url_to_postid()
that behaves similarly to your’sfind_image_post_id()
Please check the this url for your reference.