How To

Where Are Image Tables Stored in WordPress Database?

There is no image table. Images are attachment rows in wp_posts, their paths and sizes are in wp_postmeta, and the files sit in wp-content/uploads.

Shashank Dubey
·10 min read
Diagram: a WordPress image is an attachment row in the posts table, with file path, sizes and alt text in post meta and the files in the uploads folder

WordPress has no separate image table. Every image you upload is stored as a row in wp_posts with the post type attachment, its file path and size data live in wp_postmeta, and the image file itself sits on disk in wp-content/uploads/. The database never holds the picture, only the record that points to it. Image size settings are in wp_options, and the link between a post and its featured image is one more row in wp_postmeta.

We checked all of this on a test install of WordPress 7.1.4 on 18 November 2026: we imported a 3000 by 2000 pixel JPEG with WP-CLI 2.12.0, then read back every row and file it created. The output below is from that run, and the core functions we mention were checked against the WordPress source (trunk, 7.2 RC1) the same day. Your table prefix may not be wp_; swap in your own.

No Search Console queries have been recorded for this page yet, so the headings follow the questions people ask once they open phpMyAdmin looking for their images.

Is there an image table in the WordPress database?

No. WordPress treats a media file as a kind of post. That design is older than the block editor and has never changed, which is why you will not find wp_images or wp_media in a default install. A single-site install has 12 core tables, and media uses three of them. If you want the full list, our sibling guide on how many tables a default WordPress install has walks through each one.

What

Where it lives

The image file and its resized copies

On disk, in wp-content/uploads/YYYY/MM/

The attachment record (title, caption, MIME type, parent post)

One row in wp_posts with post_type = 'attachment'

File path, dimensions, every generated size, EXIF data

Rows in wp_postmeta for that attachment ID

Alt text

A row in wp_postmeta (_wp_attachment_image_alt)

Which image is a post’s featured image

A row in wp_postmeta on the post (_thumbnail_id)

Image size settings

Rows in wp_options

Plugins can add their own tables (some gallery and optimisation plugins do), but core never needs one. If you see an extra media-looking table, check which plugin created it before you touch it.

Where does WordPress store uploaded image files?

In the uploads folder, sorted by year and month of the upload. On our test site the import created eight files from one upload:

$ ls wp-content/uploads/2026/11/ | grep test-photo     # checked on 18 Nov 2026
test-photo-1024x683.jpg
test-photo-150x150.jpg
test-photo-1536x1024.jpg
test-photo-2048x1365.jpg
test-photo-300x200.jpg
test-photo-768x512.jpg
test-photo-scaled.jpg
test-photo.jpg

That is the original, six resized copies (one per registered size), and a -scaled copy. The scaled file exists because the original was wider than 2560 pixels. Since WordPress 5.3, core passes 2560 to the big_image_size_threshold filter and, when an upload is larger, saves a scaled-down copy and uses it as the main file. The original stays on disk and is recorded in the metadata as original_image.

Three things change the folder:

  • The year/month setting. Settings > Media > “Organize my uploads into month- and year-based folders” is the uploads_use_yearmonth_folders option (1 by default). Turn it off and new files land directly in uploads/.
  • Custom upload paths. The upload_path and upload_url_path options (empty by default) or the UPLOADS constant move the folder. Our guide to moving the WordPress content and uploads directory covers the constants and their traps.
  • Multisite. Each subsite after the main one writes to uploads/sites/{blog_id}/, and its rows go into its own tables, such as wp_2_posts and wp_2_postmeta.

To print the real path on any site, ask WordPress instead of guessing:

# checked on 18 Nov 2026 (WP-CLI 2.12.0)
wp eval 'print_r( wp_get_upload_dir() );'

What does WordPress save in the posts table for an image?

One row per uploaded file, using the same columns as a blog post. Some columns get a different meaning for attachments. This is the row our import created, trimmed to the columns that matter:

Column

Value on our test image

What it means for an image

ID

9

The attachment ID. Everything else points at this number.

post_type

attachment

Marks the row as a media item.

post_mime_type

image/jpeg

The file type. Filter on image/% to get images only.

post_status

inherit

Attachments inherit status from their parent post.

post_title

Test photo

The title shown in the Media Library.

post_excerpt

(empty)

The caption.

post_content

(empty)

The description field.

post_name

test-photo

The slug of the attachment page.

post_parent

0

The post it was first uploaded to. 0 means unattached.

guid

.../uploads/2026/11/test-photo.jpg

A permanent identifier set at upload. Not used to build the image URL.

Two of those surprise people. post_parent only records where the image was first uploaded; an image used in ten posts still has one parent, and an unattached image (parent 0) can be in use all over the site. And guid is not the live URL. wp_get_attachment_url() builds the URL from the _wp_attached_file meta value and the current uploads base URL, and only falls back to the guid if that meta is missing. On our test site the guid points at test-photo.jpg while the URL WordPress actually serves is test-photo-scaled.jpg.

What image data is stored in the postmeta table?

Three rows per image in a normal upload, plus a fourth if you edit the image in WordPress:

Meta key

What it holds

_wp_attached_file

The main file path relative to the uploads folder, e.g. 2026/11/test-photo-scaled.jpg.

_wp_attachment_metadata

A serialized PHP array: width, height, file, filesize, every generated size, EXIF data and the original file name.

_wp_attachment_image_alt

The alt text. Only present once alt text is set.

_wp_attachment_backup_sizes

Created when you crop, rotate or scale in the image editor, so the original sizes can be restored.

The metadata row is the one people mean when they ask where image sizes are stored. Read it with WP-CLI rather than squinting at serialized text in phpMyAdmin:

# checked on 18 Nov 2026
$ wp post meta get 9 _wp_attachment_metadata --format=json
{"width":2560,"height":1707,"file":"2026/11/test-photo-scaled.jpg","filesize":69170,
 "sizes":{"medium":{"file":"test-photo-300x200.jpg","width":300,"height":200,
 "mime-type":"image/jpeg","filesize":1678}, ...},
 "image_meta":{"aperture":"0","credit":"","camera":"", ...},
 "original_image":"test-photo.jpg"}

Notice that the sizes entries store only file names, not paths. WordPress takes the folder from _wp_attached_file and adds each size’s file name to it. The dimensions in the top level (2560 by 1707) are the scaled file’s, not the original’s 3000 by 2000.

Because the array is serialized, every string carries its length. Edit 2026/11/ to a longer path with a plain SQL REPLACE() and the stored lengths no longer match, PHP cannot unserialize the value, and WordPress treats the image as having no metadata. That is the root of several mistakes listed further down.

Where are featured images and images inside posts recorded?

Not in the attachment row. The link is stored on the side that uses the image:

Where the image is used

How the database records it

Featured image of a post or page

A _thumbnail_id row in wp_postmeta for that post, value = attachment ID

Image block in content

Inside post_content: the block comment holds "id":9 and the img tag gets class="wp-image-9" plus the file URL

Classic editor image

Only the HTML in post_content, usually with a wp-image-ID class

Site icon

The site_icon option in wp_options

Custom logo

The custom_logo theme mod, stored in the theme’s theme_mods_{theme} option

WooCommerce product gallery

A _product_image_gallery post meta row with comma-separated attachment IDs

Our test post shows the featured image row next to the meta core adds to every new post:

$ wp post meta list 10        # checked on 18 Nov 2026
post_id  meta_key        meta_value
10       _pingme         1
10       _encloseme      1
10       _thumbnail_id   9

This is why “is this image used anywhere?” has no single SQL answer. Page builders, custom fields and widgets each store image IDs or URLs in their own format, often JSON or serialized arrays inside post meta or options.

Which options control WordPress image sizes?

The sizes you set under Settings > Media are plain rows in wp_options. Our test install had the defaults:

$ wp option list --search="*size*" --fields=option_name,option_value   # checked on 18 Nov 2026
option_name           option_value
thumbnail_size_w      150
thumbnail_size_h      150
medium_size_w         300
medium_size_h         300
large_size_w          1024
large_size_h          1024
image_default_size
medium_large_size_w   768
medium_large_size_h   0

The thumbnail_crop option (1 by default) makes the thumbnail a hard crop. Two more sizes, 1536x1536 and 2048x2048, are registered in code since WordPress 5.3 and have no option rows. Themes and plugins add their own with add_image_size(), also in code. To see every size WordPress will generate on your site:

$ wp media image-size        # checked on 18 Nov 2026
name          width  height  crop  ratio
full                         N/A   N/A
2048x2048     2048   2048    soft  N/A
1536x1536     1536   1536    soft  N/A
large         1024   1024    soft  N/A
medium_large  768    0       soft  N/A
medium        300    300     soft  N/A
thumbnail     150    150     hard  1:1

Changing these numbers does not touch existing files or metadata. Only new uploads get the new sizes, until you regenerate. That is what wp media regenerate is for; it rewrites the files and the _wp_attachment_metadata row together.

How do I query image data from the WordPress database?

Here are the queries we use most, each run against our test site on 18 November 2026 (through $wpdb). Replace wp_ with your prefix and always run against a copy or with a read-only user first.

List the most recent images

SELECT ID, post_title, post_mime_type, post_date
FROM wp_posts
WHERE post_type = 'attachment'
  AND post_mime_type LIKE 'image/%'
ORDER BY post_date DESC
LIMIT 20;

The type_status_date index on wp_posts starts with post_type, so this stays fast on large libraries. Keep the LIMIT; a media library with 50,000 rows will happily flood your terminal.

Each image with its file path and alt text

SELECT p.ID,
       p.post_title,
       f.meta_value AS file,
       a.meta_value AS alt_text
FROM wp_posts p
LEFT JOIN wp_postmeta f ON f.post_id = p.ID AND f.meta_key = '_wp_attached_file'
LEFT JOIN wp_postmeta a ON a.post_id = p.ID AND a.meta_key = '_wp_attachment_image_alt'
WHERE p.post_type = 'attachment'
  AND p.post_mime_type LIKE 'image/%'
ORDER BY p.ID DESC
LIMIT 20;

The joins sit on the post_id and meta_key indexes. Rows where alt_text is NULL are images with no alt text, a quick accessibility audit.

Which posts use an image as their featured image

SELECT p.ID, p.post_title
FROM wp_postmeta pm
JOIN wp_posts p ON p.ID = pm.post_id
WHERE pm.meta_key = '_thumbnail_id'
  AND pm.meta_value = '9';

Rough check for an image inside post content

SELECT ID, post_title
FROM wp_posts
WHERE post_type IN ('post', 'page')
  AND post_status = 'publish'
  AND post_content LIKE '%wp-image-9"%';

The closing quote stops wp-image-9 matching wp-image-90. This one has no index to use, so it reads every post; fine on a few thousand rows, slow on a big site, and it misses builders that store images elsewhere.

How big the image metadata has grown

SELECT COUNT(*) AS images,
       SUM(LENGTH(meta_value)) AS metadata_bytes
FROM wp_postmeta
WHERE meta_key = '_wp_attachment_metadata';

On our test site that was 1 image and 1,448 bytes. Each extra registered image size adds an entry to every row, so sites with many theme and plugin sizes carry noticeably bigger metadata.

How do I find WordPress images with WP-CLI instead of SQL?

WP-CLI goes through the WordPress API, so it respects your table prefix, multisite and serialized data. These were checked with WP-CLI 2.12.0 on 18 November 2026:

# List image attachments
wp post list --post_type=attachment --post_mime_type=image --fields=ID,post_title,post_mime_type

# Read one image's metadata as JSON
wp post meta get 9 _wp_attachment_metadata --format=json

# The URL WordPress actually serves for it
wp eval 'echo wp_get_attachment_url( 9 );'

# Register files that were copied into uploads by FTP (use the absolute path)
UP=$(wp eval 'echo wp_get_upload_dir()["basedir"];')
wp media import "$UP"/manual/*.jpg --skip-copy

# Rebuild resized files and metadata after changing sizes
wp media regenerate 9 --yes

The --skip-copy flag tells wp media import to register files where they already are instead of copying them again. Pass an absolute path. When we gave it a relative path, it stored wp-content/uploads/manual/... in _wp_attached_file; the URL still worked through a back-compat branch, but get_attached_file() resolved to a doubled uploads/wp-content/uploads/ path that does not exist, which breaks regeneration and editing. On a migration, use wp search-replace for any URL change; it unserializes and reserializes values correctly. Our WP-CLI search-replace guide has the flags that keep the guid column out of the change.

How do I check that image records match the files on disk?

Loop over the attachments with the WordPress API and test two things for each: the file exists, and the metadata unserializes into an array. This script pages through the library 500 IDs at a time, so it works on a library of 50,000 images without loading them all into memory:

<?php
// check-media.php (checked on 18 Nov 2026, php -l clean). Run: wp eval-file check-media.php
// Reports image attachments whose file is missing or whose metadata will not unserialize.
$page = 1;
do {
	$ids = get_posts( array(
		'post_type'      => 'attachment',
		'post_mime_type' => 'image',
		'post_status'    => 'inherit',
		'fields'         => 'ids',
		'posts_per_page' => 500,
		'paged'          => $page,
		'orderby'        => 'ID',
		'order'          => 'ASC',
	) );
	foreach ( $ids as $id ) {
		$file = get_attached_file( $id );
		if ( ! $file || ! file_exists( $file ) ) {
			WP_CLI::log( "$id missing file: $file" );
		}
		if ( ! is_array( wp_get_attachment_metadata( $id ) ) ) {
			WP_CLI::log( "$id has no readable metadata" );
		}
	}
	$page++;
	wp_cache_flush_runtime(); // keep memory flat on large libraries
} while ( count( $ids ) === 500 );
WP_CLI::success( 'Done.' );

To test it, we deleted one scaled file from disk and overwrote one image’s metadata with a truncated serialized string. The script caught both, plus the bad relative-path import from the previous section:

$ wp eval-file check-media.php        # checked on 18 Nov 2026
9 missing file: /.../wp-content/uploads/2026/11/test-photo-scaled.jpg
11 missing file: /.../wp-content/uploads/wp-content/uploads/manual/ftp-photo-scaled.jpg
12 has no readable metadata
Success: Done.

For a missing file, restore it from a backup; there is nothing to regenerate from if the main file is gone. For unreadable metadata, wp media regenerate <id> --yes rebuilds the _wp_attachment_metadata row from the file, as long as the main file still exists. Run it on one ID first and compare the JSON before and after.

What happens to the image rows when I delete or move an image?

Deleting from the Media Library calls wp_delete_attachment(), which removes the posts row, its post meta, every resized file and any backup sizes in one go. Media items skip the trash by default, because the MEDIA_TRASH constant is false unless you define it, so the delete is immediate.

Going around WordPress breaks that link:

What you do

What is left behind

Delete files over FTP

Posts and meta rows still point at missing files; the library shows blank tiles and pages show broken images.

Copy files into uploads over FTP

Files with no database rows; they never appear in the Media Library until imported.

Delete rows in phpMyAdmin

Orphan files on disk, and often orphan meta rows if you only deleted from wp_posts.

Raw SQL REPLACE() on URLs

Corrupted serialized metadata wherever the string length changed.

Change the guid column

Nothing visible, but feed readers may show old items as new. WordPress’s migration documentation says not to change it.

If you are moving a site, the order that works is: copy the uploads folder, import the database, run wp search-replace for the old domain, then load a few images and the Media Library to check.

Can I store images directly in the WordPress database?

You can put binary data in a LONGBLOB column, but nothing in WordPress will use it. Every image function, the Media Library, responsive srcset output and every caching or CDN plugin expects a file on disk with a URL. Storing files in the database also makes every backup and query heavier. If disk space or offloading is the real goal, the usual answer is a plugin that copies uploads to object storage (S3, R2 and similar) and rewrites the URLs, while the attachment rows stay exactly as described above.

Common mistakes people report

These come from WordPress.org support threads; we paraphrased each problem and linked the original. Some threads are old, and we only included the ones where current WordPress still behaves the same way. Checked on 18 November 2026.

  • FTP uploads do not appear in the Media Library. A user copied large audio files into the uploads folder and found the library empty. The answer then and now: WordPress only knows about files that have attachment rows, so they need importing with a plugin or wp media import. (WordPress.org forum)
  • A cleanup plugin deletes images that were still in use. A site using a custom block builder ran a media cleaner that flagged about 1,400 images as unused and deleted many that were not. The builder stored image references in a way the scanner did not read. (WordPress.org forum, September 2022; a similar report with video thumbnails: December 2021)
  • Find-and-replace in a text editor breaks images after migration. An owner moved a 50-site multisite by editing the SQL dump in a text editor, including serialized strings, and some subsite images ended up with broken URLs. Serialized lengths are the usual culprit; WP-CLI’s search-replace handles them. (WordPress.org forum, January 2021)
  • Confusion over -scaled files. A developer who had switched the big image threshold on and off ended up with a library where half the images had -scaled copies and half did not, and regenerating did not even them out. Changes to the threshold apply when an image is processed, so check the file and original_image values in the metadata before you assume. (WordPress.org forum, September 2020)
  • Thinking the threshold blocks large uploads. A site owner believed the 2560 pixel rule stopped clients uploading big images. A forum volunteer explained that scaling happens after the upload, and the real error came from image post-processing on that server. (WordPress.org forum, December 2019)
  • Absolute paths in the attached file value. On a very old site, moving servers broke every image because _wp_attached_file held full server paths. Current core still carries back-compat code for pre-2.7 paths in wp_get_attachment_url(), so if you inherit an old site, check that value first. (WordPress.org forum)

FAQ

Which table stores images in WordPress?

The posts table, as rows with post_type = 'attachment'. Their file paths, sizes and alt text are in the postmeta table. The image files are on disk in wp-content/uploads/.

Where is the image URL stored in the WordPress database?

WordPress builds it at runtime from the _wp_attached_file meta value and the uploads base URL. The guid column holds a URL from upload time, but it is an identifier, not the source of the live URL. Images placed in content also have their URL written into post_content.

Where is the alt text of an image stored?

In wp_postmeta under the meta key _wp_attachment_image_alt for the attachment. Alt text typed into an image block is also written into that block’s HTML in the post content, so the two can differ.

Where are WordPress thumbnail sizes stored?

The size settings are in wp_options (for example thumbnail_size_w). The list of generated files for each image is inside its _wp_attachment_metadata row.

How do I find unused images in the WordPress database?

Carefully. post_parent = 0 only means unattached, not unused. Check featured image rows, post content, options and your page builder’s data, and take a full backup before any cleanup plugin deletes anything.

Do multisite subsites have separate image tables?

Each subsite has its own posts and postmeta tables (for example wp_2_posts) and its own uploads folder under uploads/sites/2/. The structure inside them is the same.

Part of the Wbcom Designs family

The all-in-one WordPress community stack

Also ours: wbcomdesigns.comvapvarun.combrndle.com