MySql查询从wordpress数据库中检索post_title,内容,特色图像链接

时间:2015-05-19 15:23:12

标签: mysql wordpress

我一直在尝试从wordpress数据库中检索基于特定CATEGORY的所有帖子的post_title,post_content和feature image链接。 (我将进行JSON解析以使用特色img和帖子标题填充android listview)

我有一个问题:

SELECT
    p1.post_title,p1.post_content,
    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
    )
WHERE
    p1.post_status="publish"
    AND p1.post_type="post"
ORDER BY
    p1.post_date DESC

这样可以正常工作,但它不会让我得到基于特定类别的结果。

有人可以帮我根据特定类别获取所有帖子的精选图片

例如:假设wp_term_taxonomy.term_id = 48

请帮助!!

1 个答案:

答案 0 :(得分:1)

试试这个

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