Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where is the post featured image link stored in the WordPress database?

Tags:

wordpress

Where is the featured image link stored in the WordPress Database? I searched in the wp_postmeta table but I cannot find out the exact post_id and links.

Is this correct? Could anyone please explain to me how it works?

like image 359
Thamaraiselvam Avatar asked Feb 21 '15 08:02

Thamaraiselvam


People also ask

Does WordPress store images in database?

Wondering where does WordPress store images in the database? All the image files that you upload are also stored in the database of your site. You can view them in the Post table as an attachment. Deleting the database files will display error on the Media section of your WordPress admin backend.

Where does the featured image of a post display?

Setting a Featured Image If a user is unable to see it, they can enable it in their screen options. By default, the Featured Image meta box is displayed in the sidebar of the Edit Post and Edit Page screens.

How do I change the featured image URL in WordPress?

Click on the settings icon at the top of the page. By default, you will start in the Block section. Click on the Document section. Scroll down until you have found the Featured Image from URL option.


3 Answers

The featured image ID is stored in wp_postmeta with a meta_key called _thumbnail_id. Example:

╔═════════╦═════════╦═══════════════╦═══════════╗
║ meta_id ║ post_id ║ meta_key      ║ meta_value║
╠═════════╬═════════╬═══════════════╬═══════════╣
║ 200     ║ 4       ║ _thumbnail_id ║ 48        ║
╚═════════╩═════════╩═══════════════╩═══════════╝

The actual thumbnail link is then contained in wp_posts with a post_type of attachment. Example:

╔════╦════════════╦═════════════════════════════════════════════════════╗
║ ID ║ post_type  ║ guid                                                ║
╠════╬════════════╬═════════════════════════════════════════════════════╣
║ 48 ║ attachment ║ http://example.com/wp-content/uploads/yourimage.png ║
╚════╩════════════╩═════════════════════════════════════════════════════╝
like image 184
rnevius Avatar answered Oct 18 '22 22:10

rnevius


I was curious, so here goes...

  • The wp_postmeta table will hold an entry for the post with meta_key of _thumbnail_id
  • the meta_value is a child post_id for the featured image
  • using that post_id, you can obtain further information from wp_posts and wp_postmeta

To put it all together, here's how to get the child wp_posts row for the featured image of post XXX...

SELECT childpost.* 
FROM wp_posts childpost 
INNER JOIN wp_postmeta parentmeta ON (childpost.ID=parentmeta.meta_value)
WHERE parentmeta.meta_key='_thumbnail_id' 
AND parentmeta.post_id=XXX;

And here's the meta data for that same image

SELECT childmeta.* 
FROM wp_postmeta childmeta 
INNER JOIN wp_postmeta parentmeta ON (childmeta.post_id=parentmeta.meta_value)
WHERE parentmeta.meta_key='_thumbnail_id' 
AND parentmeta.post_id=XXX;

The metadata will include a _wp_attached_file relative path, and a _wp_attachment_metadata containing some PHP serialized data.

like image 15
Paul Dixon Avatar answered Oct 18 '22 23:10

Paul Dixon


here my sql with full url image

SELECT concat((select option_value from wp_options where option_name ='siteurl'  limit 1),'/wp-content/uploads/',childmeta.meta_value)
FROM wp_postmeta childmeta 
INNER JOIN wp_postmeta parentmeta ON (childmeta.post_id=parentmeta.meta_value)
WHERE parentmeta.meta_key='_thumbnail_id' and childmeta.meta_key = '_wp_attached_file'
AND parentmeta.post_id = POST_ID ;

select option_name from wp_options where option_name ='siteurl' 

the result will be like this

http://yourdomain/blog-wp/wp-content/uploads/2015/04/IMG_06062014_155904.png

like image 14
Ansyori Avatar answered Oct 18 '22 22:10

Ansyori