Get Attachment (Image) ID by Image URL- WordPress Codex

Get Attachment (Image) ID by Image URL- WordPress Codex

Being a developer, some I need to perform a special task. So I research on that topic and found a new function or ways to complete my need. Recently I worked on a client site, where I have to get the attachment ID from its URL. On research, I found two ways to resolve this.

On research, I found two ways to resolve this.

  • Fetch ID from Database, using $wpdb and
  • Using WordPress Function

#1 Fetch ID from Database

Add below function to functions.php

Now you have to call this function using image source as a parameter.

That’s it.

#2 Using WordPress function ‘attachment_url_to_postid’

This is a simple way to get attachment ID using Image source URL. Just use this.

Hope this help.

Enjoy!

How useful was this post?

Click on a star to rate it!

Average rating / 5. Vote count:

As you found this post useful...

Follow us on social media!

We are sorry that this post was not useful for you!

Let us improve this post!

Share the Article

About author
Darshan Saroya

Darshan Saroya

A passionate WordPress Theme Developer. Can build a custom theme for your business.  I love to create clean and modern themes, using the power of WordPress. Here I provide the tutorials related to WordPress Development, Core PHP and HTML.

Free Bootstrap 4 Theme

Mega UI Advanced Material Design Blogging WordPress Theme

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.


Get Attachment (Image) ID by Image URL- WordPress Codex

Time to read: 1 min
0