WordPress 数据库中存储的帖子特色图片链接在哪里?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28643654/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Where is the post featured image link stored in the WordPress database?
提问by Thamaraiselvam
Where is the featured image link stored in the WordPress Database? I searched in the wp_postmetatable but I cannot find out the exact post_idand links.
WordPress 数据库中存储的特色图片链接在哪里?我在wp_postmeta表中搜索,但我找不到确切的post_id和links。
Is this correct? Could anyone please explain to me how it works?
这样对吗?任何人都可以向我解释它是如何工作的吗?
回答by rnevius
The featured image ID is stored in wp_postmetawith a meta_keycalled _thumbnail_id. Example:
特色图像 ID 存储在wp_postmeta一个meta_key称为_thumbnail_id. 例子:
╔═════════╦═════════╦═══════════════╦═══════════╗
║ meta_id ║ post_id ║ meta_key ║ meta_value║
╠═════════╬═════════╬═══════════════╬═══════════╣
║ 200 ║ 4 ║ _thumbnail_id ║ 48 ║
╚═════════╩═════════╩═══════════════╩═══════════╝
The actual thumbnail link is then contained in wp_postswith a post_typeof attachment. Example:
然后将实际的缩略图链接包含在wp_postsapost_type中attachment。例子:
╔════╦════════════╦═════════════════════════════════════════════════════╗
║ ID ║ post_type ║ guid ║
╠════╬════════════╬═════════════════════════════════════════════════════╣
║ 48 ║ attachment ║ http://example.com/wp-content/uploads/yourimage.png ║
╚════╩════════════╩═════════════════════════════════════════════════════╝
回答by Paul Dixon
I was curious, so here goes...
我很好奇,所以这里...
- The
wp_postmetatable will hold an entry for the post withmeta_keyof_thumbnail_id - the
meta_valueis a childpost_idfor the featured image - using that
post_id, you can obtain further information fromwp_postsandwp_postmeta
- 该
wp_postmeta表将举行与后一个条目meta_key的_thumbnail_id - 这
meta_value是post_id特色图片的孩子 - 使用它
post_id,您可以从wp_posts和wp_postmeta
To put it all together, here's how to get the child wp_postsrow for the featured image of post XXX...
总而言之,这里是如何为wp_posts帖子的特色图像获取子行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_filerelative path, and a _wp_attachment_metadatacontaining some PHP serialized data.
元数据将包括一个_wp_attached_file相对路径,以及一个_wp_attachment_metadata包含一些 PHP 序列化数据的路径。
回答by Ansyori
here my sql with full url image
这里我的 sql 带有完整的 url 图像
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
http://yourdomain/blog-wp/wp-content/uploads/2015/04/IMG_06062014_155904.png
回答by fdehanne
Even if rneviusanswer seemed correct, the result returned some images with this kind of URL : http://www.example.com/?attachment_id=48which was not working in my case.
即使rnevius 的答案似乎正确,结果也会返回一些带有这种 URL 的图像:http: //www.example.com/? attachment_id=48,这 在我的情况下不起作用。
Tested on Wordpress 4.9.3 :
在 Wordpress 4.9.3 上测试:
Another solution is to use _wp_attached_fileinstead :
另一种解决方案是使用_wp_attached_file:
wp_postmeta
wp_postmeta
Search for value _thumbnail_idin meta_key
搜索值_thumbnail_id的meta_key
meta_id | post_id | meta_key | meta_value
200 | 4 | _thumbnail_id | 48
wp_postmeta
wp_postmeta
Again in wp_postmeta, search for value _wp_attached_filein meta_keywhere post_idis matching the meta_valuefind in previous query
再次在wp_postmeta,_wp_attached_file在meta_keywhere 中搜索post_id与meta_value先前查询中的查找匹配的值
meta_id | post_id | meta_key | meta_value
1020 | 48 | _wp_attached_file | 2018/09/picture.jpg
Query :
询问 :
SELECT wp.ID, wpm2.meta_value
FROM wp_posts wp
INNER JOIN wp_postmeta wpm
ON (wp.ID = wpm.post_id AND wpm.meta_key = '_thumbnail_id')
INNER JOIN wp_postmeta wpm2
ON (wpm.meta_value = wpm2.post_id AND wpm2.meta_key = '_wp_attached_file')

