代码之家  ›  专栏  ›  技术社区  ›  Stephen M. Redd

如何在WordPress中选择具有特定标签/类别的文章

  •  7
  • Stephen M. Redd  · 技术社区  · 16 年前

    这是一个非常具体的问题 MySQL 执行于 文字出版社 .

    我正在尝试开发一个插件,它将显示(选择)具有特定' 标签 '并且属于特定' 类别 '(两个都是多个)

    我被告知这是不可能的,因为分类和标签的存储方式:

    1. wp_posts 包含一个日志列表,每个日志都有一个“ID”
    2. wp_terms 包含术语列表(类别和标记)。每个术语都有一个术语ID
    3. wp_term_taxonomy 有一个带有术语ID的术语列表,并且每个术语都有一个分类定义(类别或标记)
    4. wp_term_relationships 在术语和职位之间有关联

    我怎样才能加入表格,让所有贴有“核”标签的帖子都贴上 属于“类别1”类别的“交易”?

    6 回复  |  直到 6 年前
        1
  •  3
  •   JimmyPena Alex K.    12 年前

    我误解了你。我以为你想要核武器或者交易。下面应该只给你核武器和交易。

    select p.*
    from wp_posts p, wp_terms t, wp_term_taxonomy tt, wp_term_relationship tr,
    wp_terms t2, wp_term_taxonomy tt2, wp_term_relationship tr2
    wp_terms t2, wp_term_taxonomy tt2, wp_term_relationship tr2
    
    where p.id = tr.object_id and t.term_id = tt.term_id and tr.term_taxonomy_id = tt.term_taxonomy_id
    
    and p.id = tr2.object_id and t2.term_id = tt2.term_id and tr2.term_taxonomy_id = tt2.term_taxonomy_id
    
    and p.id = tr3.object_id and t3.term_id = tt3.term_id and tr3.term_taxonomy_id = tt3.term_taxonomy_id
    
    and (tt.taxonomy = 'category' and tt.term_id = t.term_id and t.name = 'Category1')
    and (tt2.taxonomy = 'post_tag' and tt2.term_id = t2.term_id and t2.name = 'Nuclear')
    and (tt3.taxonomy = 'post_tag' and tt3.term_id = t3.term_id and t3.name = 'Deals')
    
        2
  •  2
  •   Matt Rogish    16 年前

    多大的数据库结构啊。

    无论如何,我会这样做(注意,我更喜欢存在于联接中,但是如果愿意,可以将它们重新编写为联接;大多数查询分析器都会将它们折叠到相同的查询计划中)。你可能需要做一些额外的杂耍一种或另一种方式,使它工作…

    SELECT *
      FROM wp_posts p
     WHERE EXISTS( SELECT *
                     FROM wp_term_relationship tr
                    WHERE tr.object_id = p.id
                      AND EXISTS( SELECT *
                                    FROM wp_term_taxonomy tt
                                   WHERE tt.term_taxonomy_id = tr.term_taxonomy_id
                                     AND tt.taxonomy         = 'category'
                                     AND EXISTS( SELECT *
                                                   FROM wp_terms t
                                                  WHERE t.term_id = tt.term_id
                                                    AND t.name    = "Category1" 
                                               )
                                )
                      AND EXISTS( SELECT *
                                    FROM wp_term_taxonomy tt
                                   WHERE tt.term_taxonomy_id = tr.term_taxonomy_id
                                     AND tt.taxonomy         = 'post_tag'
                                     AND EXISTS( SELECT *
                                                   FROM wp_terms t
                                                  WHERE t.term_id = tt.term_id
                                                    AND t.name    = "Nuclear" 
                                               )
                                     AND EXISTS( SELECT *
                                                   FROM wp_terms t
                                                  WHERE t.term_id = tt.term_id
                                                    AND t.name    = "Deals" 
                                               )
                                )
                )
    
        3
  •  1
  •   Community paulsm4    7 年前

    所以我在我的WordPress数据库上尝试了这两个选项。我在我的文章中寻找了“tech”这一类别,标签为“perl”和“programming”。

    Eric's 在最初的select语句中添加了一个缺少的逗号后就开始工作了。它返回了3条记录。问题是,寻找“post-tag”的部分实际上是作为一个或选项工作的。我的一个帖子只有一个标签,不是两个都有。另外,最好将select区分开来。

    我试过 Matt's 但它总是返回一个空集。我可以试着“玩”它。

        4
  •  1
  •   JimmyPena Alex K.    12 年前

    试试这个:

    select p.*
    from wp_posts p, 
    wp_terms t, wp_term_taxonomy tt, wp_term_relationship tr
    wp_terms t2, wp_term_taxonomy tt2, wp_term_relationship tr2
    
    where p.id = tr.object_id
    and t.term_id = tt.term_id
    and tr.term_taxonomy_id = tt.term_taxonomy_id
    
    and p.id = tr2.object_id
    and t2.term_id = tt2.term_id
    and tr2.term_taxonomy_id = tt2.term_taxonomy_id
    
    and (tt.taxonomy = 'category' and tt.term_id = t.term_id and t.name = 'Category1')
    and (tt2.taxonomy = 'post_tag' and tt2.term_id = t2.term_id and t2.name in ('Nuclear', 'Deals'))
    

    实际上,我使用了两个相关子表的副本——术语、术语分类和术语关系。一个副本应用“类别1”限制,另一个副本应用“核”或“交易”限制。

    顺便问一下,这是一个什么样的项目,上面都是关于核交易的帖子?你想把我们列入政府的名单?;)

        5
  •  0
  •   Stephen M. Redd    16 年前

    谢谢@埃里克,它起作用了!只是一些代码更正,以备将来参考:

    • 第一个select语句在wp_term_relationship tr2之后未进入昏迷状态
    • 在同一个select语句中,必须更改以下内容:
    wp_terms t2, wp_term_taxonomy tt2, wp_term_relationship 
    
    tr2

    应该是

    wp_terms t3, wp_term_taxonomy tt3, wp_term_relationship 
    
    tr3
        6
  •  0
  •   Raghu    16 年前

    真是太好了。帮了我很多忙……

    伟大的BCoz,它给了我构建复杂查询的基本方法!

    一个小更正,针对像我这样的现成用户:)

    “wp”术语“关系”将给出“不存在错误” …使用 wp与term的关系 因为它是正确的表名。

    谢谢埃里克