WordPress数据库中存储的精选图片链接在哪里?我在wp_postmeta
表格中进行了搜索,但我找不到确切的post_id
和links
。
这是对的吗?有人可以向我解释它是如何运作的吗?
答案 0 :(得分:122)
特色图片ID存储在wp_postmeta
中,meta_key
名为_thumbnail_id
。例如:
╔═════════╦═════════╦═══════════════╦═══════════╗
║ meta_id ║ post_id ║ meta_key ║ meta_value║
╠═════════╬═════════╬═══════════════╬═══════════╣
║ 200 ║ 4 ║ _thumbnail_id ║ 48 ║
╚═════════╩═════════╩═══════════════╩═══════════╝
实际的缩略图链接随后包含在wp_posts
中,post_type
为attachment
。例如:
╔════╦════════════╦═════════════════════════════════════════════════════╗
║ ID ║ post_type ║ guid ║
╠════╬════════════╬═════════════════════════════════════════════════════╣
║ 48 ║ attachment ║ http://example.com/wp-content/uploads/yourimage.png ║
╚════╩════════════╩═════════════════════════════════════════════════════╝
答案 1 :(得分:11)
我很好奇,所以这里......
wp_postmeta
表格会为meta_key
_thumbnail_id
meta_value
是特色图片的孩子post_id
post_id
,您可以从wp_posts
和wp_postmeta
总而言之,如何获取帖子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;
这是同一张图片的元数据
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;
元数据将包含_wp_attached_file
相对路径,以及包含一些PHP序列化数据的_wp_attachment_metadata
。
答案 2 :(得分:10)
这里是我的带有完整网址图片的SQL
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'
结果将是这样的
http://yourdomain/blog-wp/wp-content/uploads/2015/04/IMG_06062014_155904.png
答案 3 :(得分:7)
即使rnevius的答案似乎正确,结果仍会返回一些具有此类URL的图像: http://www.example.com/?attachment_id=48在我的情况下不起作用。
在Wordpress 4.9.3上进行了测试:
另一种解决方案是改为使用_wp_attached_file
:
wp_postmeta
在_thumbnail_id
中搜索值meta_key
meta_id | post_id | meta_key | meta_value
200 | 4 | _thumbnail_id | 48
wp_postmeta
再次在wp_postmeta
中搜索_wp_attached_file
中的值meta_key
,其中post_id
与先前查询中的meta_value
匹配
meta_id | post_id | meta_key | meta_value
1020 | 48 | _wp_attached_file | 2018/09/picture.jpg
查询:
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')