我需要从 wp_posts 和 wp_postmeta 表中获取标题、描述(内容)和图像。
我可以使用下面的查询获取图像,但无法获取标题和内容。
SELECT childpost.*
FROM wp_jtgw_posts childpost
INNER JOIN wp_jtgw_postmeta parentmeta ON (childpost.ID=parentmeta.meta_value)
WHERE parentmeta.meta_key='_thumbnail_id'
AND parentmeta.post_id=xxxx;
谁能告诉我如何获取标题和内容以及图像?
最佳答案
我相信你想要这样的东西(从 Wordpress schema documentation 来看)
SELECT
post.id,
post.post_content,
post.post_title,
thumbnail_url.meta_value as thumbnails_url
FROM
wp_jtgw_posts as post
LEFT OUTER JOIN wp_jtgw_postmeta as thumbnail ON
post.ID = thumbnail.post_id AND
thumbnail.meta_key = '_thumbnail_id'
LEFT OUTER JOIN wb_jtgw_postmeta as thumbnail_url ON
thumbnail.meta_value = thumbnail_url.post_id AND
thumbnail_url.meta_key = '_wp_attached_file'
WHERE
post.ID=xxxx;
我已经更新了答案,以使用第二个 LEFT OUTER JOIN 返回到 _postmeta
表,以提取 post_id 的thumbnail_url(meta key = '_wb_attached_file'
)与 _postmeta
表的第一个 LEFT OUTER JOIN 中找到的 thumbnail_id
相关。
关于php - 如何在单个查询中获取帖子标题、描述和图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47036010/