• Hello. I’m trying to use WP attachment/image functions to manipulate images whose paths I get from a custom field. I need to retrieve the ID for each image based on this path. I’m assuming I need to write a SQL query but I don’t have the expertise. Can anyone give me a hand or some direction?

Viewing 2 replies - 1 through 2 (of 2 total)
  • If you want to get the image attachment id’s from all your posts, then your SQL would be like this:

    SELECT ID FROM 'wp_posts' WHERE post_type = 'attachment' AND post_mime_type LIKE 'image/%'

    Posting just for anyone looking for an answer. Each post, images and other attachments included, has an associated GUID that is its complete URL path. So, just pull the “post” with the associated GUID:

    SELECT id FROM wp_posts WHERE guid = ‘https://myblogdomain.com/full-path-to-image.jpg’

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Get image ID from path’ is closed to new replies.