Blogging & content · Guide
How to Get Image ID in WordPress (Step by Step)

Short answer
Open Media, then Library, click the image and look at the browser address bar. The number after item= is the image ID. In list view, the number after post= in the Edit link is the same ID, and in the block editor it appears in the wp-image class.
Key takeaways
- Every uploaded image is an attachment with its own numeric ID.
- The Media Library address bar shows the ID after item= or post=.
- Image block markup shows the ID in its id value and wp-image class.
- get_post_thumbnail_id() returns a post's featured image ID.
- attachment_url_to_postid() finds an ID from a file URL.
- Images outside the Media Library have no attachment ID.
How do you get an image ID in WordPress?
Open Media, then Library, click the image, and read the number after item= in the browser address bar; that number is the image's attachment ID.
Every file you upload to WordPress is stored as an attachment post with its own numeric ID. Plugins, shortcodes, page builders and custom code often ask for that number.
There are several ways to find it, depending on whether you are in the Media Library, the block editor, the front end or your own code.
The steps below start with the quickest dashboard method and move on to options for developers.
Step 1: How do you find the image ID in the Media Library grid view?
Go to Media, then Library, click any image to open its details window, then look at the address bar, where the ID appears as item= followed by a number.
For example, an address ending in upload.php?item=245 means the image ID is 245.
This works for any file type in the library, not just images. PDFs, videos and audio files have attachment IDs in the same place.
If the address bar does not change when you click an image, refresh the page and try again. Some browser extensions and admin plugins can interfere with the details window.
The details window also shows the file name, upload date, size and alt text. It does not display the ID as a labeled field, which is why the address bar is the easiest place to check.
The WordPress Media Library screen documentation explains the grid and list views.
Step 2: How do you find the image ID in list view?
Switch the Media Library to list view, hover over an image and look at the Edit link, or click it and read the number after post= in the address bar.
The edit address looks like post.php?post=245&action=edit. The number after post= is the ID.
List view is handy when you need several IDs. Hover over each row in turn and check the link preview in the bottom corner of your browser.
Some small plugins add an ID column to the Media Library list. That saves time if you look up IDs often, but it is not required.
Step 3: How do you find the image ID in the block editor?
Select the Image block, open the options menu and choose Edit as HTML, then look for the number in wp-image- and in the block's id attribute.
The block markup looks something like this:
<!-- wp:image {"id":245,"sizeSlug":"large"} -->
<figure class="wp-block-image size-large"><img src="..." class="wp-image-245"/></figure>
<!-- /wp:image -->
Both the "id":245 value and the wp-image-245 class give you the same number.
Switch back with Edit visually when you are done. You can also use the Code editor from the editor's main options menu to see the markup for the whole post.
Our guide on how to edit a block in WordPress shows where the block options menu sits.
Step 4: How do you find the image ID on the front end?
Right-click the image on your live page, choose Inspect, and look for a class like wp-image-245 on the image tag.
Browser developer tools show the HTML for the element you clicked. Chrome's DevTools DOM guide explains how to inspect and search elements.
Most themes keep the wp-image- class when they output images from post content. Featured images and images from page builders may use different classes.
If no ID class appears, search the Media Library for the file name shown in the image URL instead.
Step 5: How do you get the featured image ID?
In code, use get_post_thumbnail_id() with the post ID; in the dashboard, click the featured image in the post sidebar and check the Media Library address for its ID.
The featured image is stored as a custom field that holds the attachment ID. The function reads that value for you.
$image_id = get_post_thumbnail_id( get_the_ID() );
The get_post_thumbnail_id() reference covers its parameters and return value.
If you need to set one first, see our guide on setting a featured image in WordPress.
Step 6: How do you get an image ID from its URL in code?
Use attachment_url_to_postid() with the full image URL, which returns the attachment ID, or zero if WordPress cannot match the URL.
$image_id = attachment_url_to_postid( 'https://example.com/wp-content/uploads/2025/05/photo.jpg' );
This works best with the original file URL. Resized versions with dimensions in the file name, such as photo-300x200.jpg, may not match.
The attachment_url_to_postid() reference notes how matching works.
Once you have the ID, functions like wp_get_attachment_image() can output the image at any registered size with proper responsive attributes.
How do you find image IDs with the REST API or WP-CLI?
Request the media endpoint of the REST API, or run a WP-CLI command that lists attachments with their IDs and titles.
The REST API media endpoint returns each item's ID, title and URLs. Visiting /wp-json/wp/v2/media on your site lists recent public media, and the REST API media reference lists the search and filter options.
On servers with WP-CLI installed, this command lists attachment IDs and titles:
wp post list --post_type=attachment --fields=ID,post_title
These methods suit developers handling many images, such as during a migration.
What can you do with an image ID once you have it?
You can pass the ID to shortcodes, plugin settings and theme functions that need a specific image, which is more reliable than pasting a file URL.
The classic gallery shortcode takes a list of IDs, such as [gallery ids="245,246,247"]. Each number points to one image in the Media Library.
Custom field plugins often store images as IDs too. When an image field returns an ID, you pass it to a function such as wp_get_attachment_image() to print the picture at the size you want.
Using IDs has a real advantage. If you replace or resize the file, WordPress still knows which attachment you mean, and it can output the correct responsive sizes.
URLs are more fragile. They break when you change domains, move to HTTPS or switch to a CDN, while the ID stays the same.
Page builders also use IDs behind the scenes. When you choose an image in a widget, the builder saves its ID along with the URL so it can load the right size later.
Which method should you use?
Use the Media Library address for one-off lookups, the block markup when editing a post, and code, the REST API or WP-CLI when you need IDs inside themes, plugins or scripts.
| Where you are | Method | What to look for |
|---|---|---|
| Media Library grid | Click image, check address bar | item= number |
| Media Library list | Hover or open Edit link | post= number |
| Block editor | Edit as HTML or Code editor | “id” value or wp-image- class |
| Live page | Inspect element | wp-image- class |
| Theme or plugin code | get_post_thumbnail_id() or attachment_url_to_postid() | Returned integer |
| Bulk lists | REST API or WP-CLI | ID field |
Why can't you find an image ID?
The image may be loaded from outside the Media Library, such as a theme folder, an external URL or a page builder background, in which case it has no attachment ID.
Only files uploaded through WordPress get an attachment record. Images copied into folders by FTP do not appear in the Media Library until they are imported.
If the Media Library will not open properly, fix that first. Our guide to the Media Library not loading covers the common causes.
Missing images on the front end are a different problem. See images not showing in WordPress for those fixes.
Do image IDs change when you move a site?
Image IDs usually stay the same when you migrate the full database, but they can change when you import content into a site that already has posts.
Full migration tools copy the database as it is, so IDs match. The WordPress Importer creates new attachment posts, which may get new numbers.
After an import, check any shortcodes, galleries or custom fields that store image IDs. They may need updating to the new numbers.
Keep a quick list of the IDs your theme settings or widgets rely on, such as a logo or default header image. That makes it easy to spot and fix any that changed after a move.
Frequently asked questions
Where is the image ID in the WordPress Media Library?
Click the image in grid view and look at the browser address bar. The number after item= is the ID. In list view, hover over the image or open its Edit link, and the number after post= is the ID.
Is the image ID the same as the attachment ID?
Yes. WordPress stores each uploaded file as an attachment post, so the image ID and the attachment ID are the same number. Plugins and code may use either name, but they always refer to that one value.
How do I get the featured image ID of a post?
In code, call get_post_thumbnail_id() with the post ID. Without code, open the post, click the featured image in the sidebar to open the media window, and check the address bar of the Media Library for the item number.
Why does attachment_url_to_postid return 0?
WordPress could not match the URL to an attachment. This often happens with resized image URLs that include dimensions in the file name, images on a CDN domain, or files that were never uploaded through the Media Library. Try the original file URL.
Can I see image IDs without opening each image?
Yes. Switch the Media Library to list view and hover over each item to see the Edit link with its ID. Developers can list many IDs at once with the REST API media endpoint or a WP-CLI command.
Sources
- Media Library Screen WordPress.org Documentation
- get_post_thumbnail_id() WordPress Developer Resources
- attachment_url_to_postid() WordPress Developer Resources
- REST API Media reference WordPress Developer Resources
- WP-CLI WP-CLI
- Get started with viewing and changing the DOM Chrome for Developers
Is your product missing, or something out of date?
Tell us what to add or correct. We read every note and reply when we need a source. A message does not buy a ranking.
Thanks — we review requests within 3 business days.
Related guides
About the editors
Researched from the primary sources listed above and checked against our editorial rules before publishing.


