I worked this out eventually by tracing through the code for get_attached_file() to see where it was getting the filename from, and reverse engineered the following:
function get_attachment_id_by_filename($filename) {
global $wpdb;
$sql = $wpdb->prepare("SELECT * FROM $wpdb->posts WHERE post_type="attachment" and guid like %s order by post_date desc", "%$filename");
$attachments = $wpdb->get_results($sql, OBJECT);
return $attachments[0]->ID ?? false;
}
Note that it is possible the query will return more than one row if the same file basename exists in different media library folders. This routine will however only return the most recent found.
This works for everything I can throw at it so far, but welcome any issues people may be aware of with this solution.
Related Posts:
- remove links from images using functions.php
- How to add classes to images based on their categories?
- Auto delete WordPress images/thumbnails (all sizes) and featured after X days/hours, or similar?
- How to add a rel attribute to images that contains their categories?
- Best way to programatically add “rel” attributes to page and post images
- What is the “icon” parameter in wp_get_attachment_image_src used for?
- Find first image on paginated post for Pinterest
- How do I permanently Disable Attachment Post URL
- Can’t check if a post has thumbnail adding filter to get_post_metadata()
- Image rotation fails to regenerate custom sized thumbnail
- Display images that are not in the content
- Can’t filter wp_get_attachment_link
- Get ID of first image attached to a post
- How to stop wordpress from injecting hard-coded style into image attachments with captions
- How to hide image-url if no attachment?
- attachment page template? only show attachments for current post?
- What are the meta fields for an attachment?
- Remove attachment page for audio media type only
- Get Attachment Category Name
- How d0 i get the number of attachments in the post
- Different image using srcset function
- Auto delete content in specific folder inside media library
- WP_Query for Attachments not working as expected
- How to update an image attachment’s alt text from a custom field when saving a post?
- Remove all video attachment, both files and post/postmeta from database?
- Open image size links in a page instead of direct image link
- Set thumbnail from URL, by grabbing image in functions.php
- Disable Attachment Page Except for Category
- How to run a function every 5 minutes?
- Upload Multiple Files With media_handle_upload
- Add custom template page programmatically
- How to override admin-bar style
- Woocommerce add to simple product attribute programmatically [closed]
- How can I tell if I’m on a login page? [duplicate]
- Get Category and Excerpt From wp_get_recent_posts
- Order get_users() by last login date. Is it possible?
- Error after editing functions.php
- How do I properly register bootstrap JavaScript into WordPress functions.php?
- Function to Download External Images to My Site
- Manipulate Output of wp_list_something: select menu instead of li’s
- Set WordPress Featured Image For All Post Slugs Matching Image File Name in Specified Directory
- How to call a function on particular page like ‘contact-us’ from function.php of child theme
- Detect Safari desktop browser and include the detection in a shortcode
- Is It wrong to use oop approach on functions.php?
- how to fix Warning: Use of undefined constant _ – assumed ‘_’ (this will throw an Error in a future version of PHP)
- restore_current_blog required after switch_to_blog, if I use that in a function?
- ‘is’ functions and ‘get_query_var’ not working
- How Can I Expand the WordPress Customize window without any Plugin?
- “is_new_day()” alternative for years?
- How to hide a plugin metabox for non admins when a user adds a new post
- Getting Featured Image Caption to Only Show if Populated
- Where do I add html code to the menu div?
- How do I create page navigation linking to each H2 within the page? [closed]
- Add function to every post?
- Allow Post Author to be 0 on Update
- How do I hide or remove ‘Category’ from wordpress breadcrumbs
- Display ‘BuddyPress Follow’ follow button to non-logged in users [closed]
- wp-admin won’t load after setting wp-login custom url
- jQuery does not work
- Function naming convention
- Generate featured images old posts
- debugging errors.. how to remedy?
- Where do i create my own function in wp-admin
- Use /prefix/postname as a slug in post_name?
- Send email to admin user when custom post type is created
- Pass variable from action back to template
- Some questions about how proper add 2 CSS file in a WordPress theme?
- Test if a post exists by category and date
- Where to call my pagination function?
- How do I pull excerpts from pages?
- Output loop to function return?
- Can’t add_action to ‘save_post’ and get it to fire
- I changed Functions.php and now I get “cannot decode raw data NSURLErrorDomain:-1015” (not blank)
- How to load jQuery in TOP of wp_footer?
- How to fetch custom post type by variable date?
- two search forms on the same page
- Getting out side of wp root folder from function.php using absolute path
- current_page_item is missing inside wp_nav_menu
- Change default upload images size for contributors
- Strange error from functions.php files (wp_register_script)
- file.php not loaded?
- How to setup a popup registeration page in wordpress with function.php
- How to add if statement on WordPress shortcode output
- Why does modifying the “read more” link remove the link class?
- Woocommerce product price change
- Allow user only create specific tags
- How to display different blocks for mobile and desktops
- Help wiht adding fullcalendar.io to a WordPress page
- WP Enqueue Script Error
- Enqueue JS + CSS via a child theme functions.php file?
- Move product attributes after summary on single product page
- Javascript and Stylesheet in child page
- How to add number to wordpress count function?
- Why are some custom javascript files working but some are not
- Custom Nested WordPress Comments with avatar
- Functions file mods and CPU
- How to overwrite ‘read more” text for artmag theme
- How to display milliseconds instead of seconds using timer_stop function? [closed]
- Different ads on every website on multisite environment
- How to get variable from other function inside class function using add_action for Ajax call