MYSQL检索特色图像和帖子标题

时间:2016-07-11 08:59:28

标签: php mysql wordpress

我想检索"精选图片的缩略图版本" 这是我的代码,但它正在拉动大图像。

SELECT a.post_title title, max(c.guid) img_url, a.ID id, a.post_name
FROM wp_posts a    
LEFT JOIN
    (select post_parent, max(post_date_gmt) as latest_image_date
     from wp_posts
     where post_type='attachment'
     GROUP BY post_parent) b 
         on a.id=b.post_parent    
LEFT JOIN
    wp_posts c
    on c.post_parent=a.id 
     and c.post_type='attachment' 
     and b.latest_image_date = c.post_date_gmt
WHERE c.guid IS NOT NULL    
GROUP BY a.post_title
ORDER BY a.ID

1 个答案:

答案 0 :(得分:0)

这个查询可能适用于U.它对我来说非常好。

SELECT p1.*, wm2.meta_value 
FROM wp_posts p1 
LEFT JOIN
    wp_postmeta wm1 ON (
        wm1.post_id = p1.id
        AND wm1.meta_value IS NOT NULL
        AND wm1.meta_key = '_thumbnail_id'
    )
LEFT JOIN
    wp_postmeta wm2 ON (
        wm1.meta_value = wm2.post_id
        AND wm2.meta_key = '_wp_attached_file'
        AND wm2.meta_value IS NOT NULL
    )
LEFT JOIN
    wp_term_relationships wtr ON (
        object_id=p1.id
    )
WHERE
    p1.post_status='publish'
    AND p1.post_type='post'
    AND `term_taxonomy_id`=?
ORDER BY p1.post_date DESC
LIMIT 0,10