Support » Everything else WordPress » find attachment id from image url

  • I’ve available the URL of an image only – no size information, no class etc. I need to find out the attachment id of the image.
    This works fine using attachment_url_to_postid() as long as the URL is identical to the image URL you can find in the media library, e.g. ‘ https://www.example.com/wp-content/uploads/2021/04/image-scaled.jpg ‘.
    But it fails as soon as you use the URL of the original ‘image.jpg’ or of the image of a different size like ‘image-300×200.jpg’.
    How can I find out the attachment id (that obviously fits to ‘image-scaled.jpg’) by providing ‘image.jpg’ or ‘image-300×200.jpg’.
    Thanks a lot for your assistance in advance.

    • This topic was modified 2 days, 12 hours ago by tux4u2.
Viewing 2 replies - 1 through 2 (of 2 total)
  • Joy

    (@joyously)

    If the function no longer works as advertised, because the image scaling was added later, you should write a ticket. https://core.trac.wordpress.org/

    Thread Starter tux4u2

    (@tux4u2)

    @joyously
    Thanks for your reply. I don’t know if the function already worked in the past and therefore the problem should become subject of a ticket.
    I don’t think that the image scaling added later is the reason. The problem occurs regardless of the fact whether the image has been scaled on upload or not. Even if there is the original image in the media library (‘image.jpg’), the function call using ‘image-300×200.jpg’ fails.
    I’m still hoping that somenone can tell me a solution.

Viewing 2 replies - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.