选择同时包含三个术语的帖子

时间:2013-10-05 08:50:23

标签: wordpress filter

在wordpress中,我怎样才能获得同时归为三类的帖子ID?

示例:

object_id      term_taxonomy_id
-------------------------------
   6913            65
   7000            80
   7500            65
   2500            20
   6913            67
   6913            70
   5555            65
   5555            67
   6913            66

所以同时有65,67和70条的帖子是6913.

1 个答案:

答案 0 :(得分:0)

使用查询,您可以获得具有这三个类别的帖子

SELECT * FROM `wp_posts` p
LEFT JOIN `wp_term_relationships` tr ON (p.ID = tr.object_id)
INNER JOIN wp_term_taxonomy tt ON (tt.term_taxonomy_id = tr.term_taxonomy_id )
WHERE p.post_status='published' AND tt.term_id IN( 65,67,70)
HAVING COUNT(tr.object_id) =3  

以上查询将返回所有包含这三个类别的帖子65,67,70

您可以使用wordpress wpdb类来运行查询

global $wpdb;
$posts=$wpdb->get_results("SELECT * FROM `wp_posts` p
    LEFT JOIN `wp_term_relationships` tr ON (p.ID = tr.object_id)
    INNER JOIN wp_term_taxonomy tt ON (tt.term_taxonomy_id = tr.term_taxonomy_id )
    WHERE p.post_status='published' AND tt.term_id IN( 65,67,70)
    HAVING COUNT(tr.object_id) =3");