获取包含特定类别附件的帖子?

时间:2010-05-20 22:51:00

标签: sql mysql wordpress join

我需要检索一个帖子列表,这些帖子包含(至少)一个属于WordPress中某个类别的附件。

我自己使用WordPress默认方法制作的附件和类别之间的关系。

这是我现在正在运行的查询:

SELECT post.*
FROM `bma_posts` AS post
WHERE
    EXISTS (
        SELECT 1
        FROM `bma_posts` AS attachment
        JOIN `bma_term_relationships` AS relationship ON
            relationship.`object_id` = attachment.`ID`
            AND
            relationship.`term_taxonomy_id` IN (17,15,16,5)
        WHERE
            attachment.`post_parent` = post.`ID`
            AND
            attachment.`post_type` = 'attachment'
    )
    AND
    post.`post_type` = 'post'
    AND
    post.`post_status` = 'publish'
ORDER BY post.`post_date` DESC
LIMIT 3

现在的问题是我无法获得附件。ID知道“谁”在查询中包含该帖子。

1 个答案:

答案 0 :(得分:1)

Select ...
From wp_posts As P
Where Exists    (
                Select 1
                From wp_posts As P1
                    Join wp_term_relationship As WTR1
                        On WTR1.object_id = P1.ID
                            And WTR1.term_taxonomy_id In(3)
                Where P1.post_parent = P.Id
                    And P1.post_type = 'attachment'
                )
    And P.post_type = 'post'
Order By p.post_date DESC
Limit 15