【问题标题】:Select post that has three terms at the same time选择同时包含三个词的帖子
【发布时间】:2013-10-05 17:51:46
【问题描述】:

在 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。

【问题讨论】:

    标签: wordpress filter


    【解决方案1】:

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

    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  
    

    以上查询将返回所有具有这 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");
    

    【讨论】:

      猜你喜欢
      • 2015-06-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多