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!


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.

Get Updates

%d bloggers like this: