我想有一个在wordpress数据库上的mysql查询,恢复标题和最后6个帖子的第一个图像链接。
Wordpress的核心功能是不允许的,因为我想在外部网站上展示它们。换句话说,我需要纯mysql查询。
我可以这样显示标题:
$result = mysql_query("select * FROM wp_posts WHERE post_status='publish' AND post_type='ad_listing' ORDER BY id desc limit 6" ,$db);
while ($records = mysql_fetch_assoc($result)) {
echo '<li>'.$records['post_title'] ."</li>";
}但是如何恢复附加到这些帖子的第一个镜像(如果存在)?
发布于 2013-03-21 06:06:58
对于wp_posts表中的图像记录,post_parent是否指向已发布的页面?我的不支持,如果你的也不支持,那么你需要搜索每个已发布页面的post_content字段,寻找img标签(或者你的图片使用的任何标签)。
从我读过的其他文章中可以看出,有时图像的post_parent指向父页面。如果您的数据库是这样的,那么您应该能够执行以下操作:
SELECT
post.id AS post_id,
post.post_title,
post.guid AS post_url,
image_detail.id AS image_id,
image_detail.post_title AS image_title,
image_detail.guid AS image_url
FROM wp_posts AS post
LEFT JOIN (
SELECT post_parent, MIN( id ) AS first_image_id
FROM wp_posts
WHERE post_type = 'attachment'
AND post_mime_type LIKE 'image/%'
GROUP BY post_parent
) AS image_latest
ON post.id = image_latest.post_parent
LEFT JOIN wp_posts AS image_detail
ON image_detail.id = image_latest.first_image_id
WHERE post.post_status = 'publish';https://stackoverflow.com/questions/15534833
复制相似问题