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

10

这是关于在WordPress中实现的MySQL数据库的一个非常具体的问题。

我正在尝试开发一个插件,展示(选择)有特定“标签”和属于特定“分类”(都有多个)的文章。

有人告诉我,由于分类和标签的存储方式,这是不可能的:

  1. wp_posts 包含文章列表,每篇文章都有一个“ID”
  2. wp_terms 包含术语列表(包括类别和标签)。每个术语都有TERM_ID
  3. wp_term_taxonomy 列出了其TERM_ID的一组术语,并为每个术语定义了一个分类或标签
  4. wp_term_relationships 包含术语和文章之间的关联

如何连接这些表以获取所有既有标签“Nuclear”又有标签“Deals”还属于分类“Category1”的文章呢?

6个回答

5

我误解了你的意思。我以为你想要核能或交易。下面应该只给你核能和交易。

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')

3

试试这个:

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'))

基本上我正在使用两个相关子表 - terms、term_taxonomy 和 term_relationship。一个副本应用“Category1”限制,另一个副本应用“Nuclear”或“Deals”限制。
顺便问一下,这是什么样的项目,所有帖子都关于核交易?你试图让我们进入某个政府名单吗? ;)

这是一个非常棒的解决方案。 - Lenin Zapata

2

这个数据库结构很糟糕。

不过,我会做出类似于以下的操作(请注意,我更喜欢使用 EXISTS 而非连接,但如果你愿意,你可以将它们重写为连接;大多数查询分析器都会将它们折叠到相同的查询计划中)。你可能需要做一些额外的调整才能使其正常工作...

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" 
                                           )
                            )
            )

1

我在我的WordPress数据库上尝试了两个选项。我查找了使用“Perl”和“编程”标签的文章中的“Tech”类别。

Eric's一旦我在初始查询语句中添加了一个缺失的逗号,就可以工作了。它返回了3条记录。问题是寻找“post_tag”的部分实际上是作为OR选项工作的。我的一篇文章只有一个标签而不是两个。此外,最好使用SELECT DISTINCT。

我尝试了Matt's的版本,但它一直返回一个空集。我可以尝试“搭配”一下它。


0

感谢 @Eric,它有效了!只需进行一些代码更正以供将来参考:

  • 第一个 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

0

真的非常好的答案.. 对我很有帮助..

非常好,因为它为我提供了构建复杂查询的基本方法!

一个小修正,对于像我这样的准备用户 :)

“wp_term_relationship”会给出“不存在错误”的错误 ..使用wp_term_relationships作为正确的表名。

谢谢Eric


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接