Based on my research, you would need to run a query on directly on the database using WPDB. So you would do something like this:
Add this in your functions.php
file:
// retrieves the attachment ID from the file URL
function get_image_id($image_url) {
global $wpdb;
$the_attachment = $wpdb->get_col($wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE guid='%s';", $image_url ));
return $the_attachment[0];
}
And then you can apply it wherever you like this way:
$attachment_url="http://example.com/wp-content/uploads/2020/10/28/just_for_the_lolz.jpg";
$attachment_id = get_image_id($image_url);
$attachment_id
will have the attachment ID so you can do whatever you want with it.
Related Posts:
- Get All Images in Media Gallery?
- How to add subfolders in media library for better organisation?
- How to display .ico files in the media library
- List of available events for wp.media
- Uploading Images to Media Library via wp_handle_sideload() fails
- Bind event on Media gallery elements WordPress
- Add class=”media_type” when media is inserted into editor
- How can I list URLs of all audio files within my media gallery?
- Migrating to HTTPS only, most media library links need updating?
- Failing to open advanced-cache.php which is breaking Media Library
- Filter pictures in the media library [closed]
- Remove the columns in media library
- How to save attachment data?
- How to modify Media Library images
- WordPress 3.5 Media Manager – Reorder Selectbox items on Media Library Tab [duplicate]
- Inserting a Download Link in the Quick Edit Actions of the Media Library?
- How do I remove the entire Media section from the main WordPress navigation without just hiding it?
- Is there any way to add images to the Media Library through a path on the server?
- Media Library page super slow, loading up full quality images
- Share media among different domains
- Where is the source of the media items filter
- Bulk Import files on Media Library from CSV
- Media gallery thumbnails 1px
- “Insert Into Post” For Custom Button
- How to have changes made in media library to be reflected on posts/pages
- Display image file size in media library
- Media_handle_upload with custom upload folder?
- Remove the ‘Attachment Details’ section of the wordpress media manager
- remove_menu_page breaks “Add media”
- is there any hooks or action to fire function of insert into post in media library?
- Featured images get shrunken
- Organize media uploads in folders – I have a physical access to (my) server
- Pulling images from a subdomain
- Get image URL from media library “insert from url”
- Media gallery: remove “insert into post” button keeping “Insert gallery” button
- Media Library not showing images properly
- Media Content disapeared after upgrade WP core to 3.5.1
- The best way to protect uploaded media in WordPress?
- Media Library Grid View Custom Filter
- how to remove thumbnail and LiteSpeed Optimization column in media library list-view?
- Not able to extend wp.media.view.AttachmentsBrowser
- Custom Media Modal and uploading
- wp_get_attachment_image_src returns the wrong image
- Media Library No Longer Displays Images
- Media Library SSL
- Change Media Library thumbnails for non-image files to include the post title instead of the filename?
- attachment media-template data model (data.size.url)
- Media library and posts list problems with WordPress on IIS
- How do you alter the default WordPress thumbnail?
- “add from media library” tab is super slow [closed]
- Reorder attachements in the media library
- Adding Alt Tag Column to Media Library List
- How to modify the attachment info text on gallery tab or media page?
- Add media button always inserts media on the next wp_editor instance when created dynamically
- Adding Alt Tag and Dimensions columns to Media Library
- Media Library Latency, Lag, CRON?
- Is it Helpful to transfer all media library to another host? [closed]
- Media library on another server?
- Is there a way to determine if the media-iframe is visible?
- What would be the URL to display the WordPress image editor with an image from the media library?
- Prevent other editors from viewing images of other editors in Add Media Popup
- Media attachment date and post date
- Windows Live Writer inconsistent with Media Library
- Is there a way to programmatically pass a category to the Media Library selector?
- User media folder for each users on registration
- Larger preview thumbnails in Media Library
- How can I no-index specific PDF files in my Avada WordPress media library so that they don’t appear in search results?
- Admin Custom Plugin List Files in admin Table
- Media Library http to https
- Filter media library by custom postmeta
- How to limit the amount of images uploaded to the media library within post.php
- Change thumbnail size for Media Library
- Adding Facebook Open Graph Code does not work for Media Library Attachment Pages
- Media library corrupted after upgrading to 4.9.6
- query that alternates text posts with images from media gallery
- Can multiple sites under one folder structure share media directories?
- WordPress Media Delete Logs
- WordPress not using edited image
- How to display the date a directory folder on ftp was modified
- When I’m in post editor and click on ‘Add media’, why won’t it load the media library with this code?
- Create gallery doesn’t show uploaded images
- Media Library not showing images when adding to Posts (but retrieving image info via ajax)
- WordPress 3.5 Media Manager – Change loaded image Size [duplicate]
- How use Videos from another directory?
- Remove (Unattached) Attach from “Uploaded to” in Media Library
- Uploading pictures to the Media Library: “Specified file failed upload test.”
- Use the “Media Library” in Frontend
- Why is my upload / Select media library button empty?
- WordPress Media Library
- Create media library feed
- Broken image links in media library after restore
- Store images and videos category wise and fetch them in json format
- Add Filename Colum to Media Library
- create a user’s owned folder on user registration
- How to bulk edit taxonomies for attachments
- wp cli media commands not working
- Block non logged in users from accessing the media library
- Remove Edit Attachment Button in WordPress Mediathek -> see Screenshot
- in classic editor, is there a way to add media lib files to the content search when creating a link?
- Editing alt text in media library