如何在WordPress中选择带有特定标签/类别的帖子
这是关于在WordPress中实现的mysql的一个非常具体的问题。
我正在尝试开发一个插件,该插件将显示(选择)具有特定"标签"且属于特定"类别"(多个)的帖子
有人告诉我这是不可能的,因为类别和标签的存储方式是:
- wp_posts包含一个帖子列表,每个帖子都有一个" ID"
- wp_terms包含术语列表(类别和标签)。 Eac条款具有TERM_ID
- wp_term_taxonomy具有其TERM_ID的术语列表,并为每个术语定义了分类标准(类别或者标签)
- wp_term_relationships具有条款和帖子之间的关联
如何加入表格以获取所有标签也同时属于" Category1"类别的标签" Nuclear"和" Deals"?
解决方案
回答
多么粗暴的数据库结构。
无论如何,我会做这样的事情(请注意,我更喜欢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" ) ) )
回答
试试这个:
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'))
本质上,我正在使用相关子表术语,term_taxonomy和term_relationship的2个副本。一份适用" Category1"限制,另一份适用" Nuclear"或者" Deals"限制。
顺便说一句,这是一个与核交易有关的所有项目?我们想让我们进入某些政府名单吗? ;)
回答
因此,我在WordPress数据库上尝试了这两个选项。我在我的帖子中使用标签" Perl"和" Programming"寻找" Tech"类别。
我在初始选择语句中添加了一个逗号后,埃里克(Eric)就工作了。它返回了3条记录。问题是正在寻找" post_tag"的部分实际上是作为OR选项工作的。我的一篇文章只有一个标签,而没有两个标签。同样,使SELECT DISTINCT成为好选择。
我尝试了Matt的版本,但是它一直返回一个空集。我可能会尝试"玩弄"它。
回答
我误会了你我以为你想要核交易。以下应该只给我们核和交易。
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')
回答
谢谢@Eric,它有效!仅作一些代码更正,以供将来参考:
- wp_term_relationship tr2之后,第一个选择语句缺少逗号
- 在相同的选择状态中,必须更改以下内容:
wp_terms t2, wp_term_taxonomy tt2, wp_term_relationship tr2
应该
wp_terms t3, wp_term_taxonomy tt3, wp_term_relationship tr3
回答
真的很棒的答案..对我有很大帮助。
太棒了,它为我提供了建立复杂查询的基本方法!
一个小小的更正,适合像我这样的现成用户:)
" wp_term_relationship"将给出"不存在错误"
..使用wp_term_relationships,因为它是正确的表名。
谢谢埃里克