Php 如何在WordPress中选择带有特定标签/类别的帖子
这是一个关于在WordPress中实现的MySQL的非常具体的问题 我正在尝试开发一个插件,该插件将显示(选择)具有特定“标记的帖子,并且属于特定的“类别”(都是多个) 我被告知这是不可能的,因为类别和标签的存储方式:Php 如何在WordPress中选择带有特定标签/类别的帖子,php,mysql,sql,wordpress,plugins,Php,Mysql,Sql,Wordpress,Plugins,这是一个关于在WordPress中实现的MySQL的非常具体的问题 我正在尝试开发一个插件,该插件将显示(选择)具有特定“标记的帖子,并且属于特定的“类别”(都是多个) 我被告知这是不可能的,因为类别和标签的存储方式: wp_posts包含一个帖子列表,每个帖子都有一个“ID” wp_terms包含术语列表(类别和标记)。每个术语都有一个术语ID wp_term_taxonomy有一个术语列表及其术语ID,并为其中的每一个术语(类别或标记)提供分类定义 wp\u term\u关系在术语和帖子之间
wp_posts
包含一个帖子列表,每个帖子都有一个“ID”wp_terms
包含术语列表(类别和标记)。每个术语都有一个术语IDwp_term_taxonomy
有一个术语列表及其术语ID,并为其中的每一个术语(类别或标记)提供分类定义wp\u term\u关系
在术语和帖子之间有关联我如何加入表格以获得所有带有标签“Nuclear”和“Deals”的帖子,这些标签也属于类别“Category1”?多大的数据库结构啊 无论如何,我会这样做(注意,我更喜欢EXISTS而不是JOIN,但是如果愿意,可以将它们重新编写为JOIN;大多数查询分析器都会将它们折叠到相同的查询计划中)。你可能不得不以这样或那样的方式做一些额外的杂耍来使它工作
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'))
基本上,我使用了相关子表的两个副本——terms、term_分类法和term_关系。一份适用“类别1”限制,另一份适用“核”或“交易”限制
顺便说一句,这是一个什么样的项目,上面都是关于核交易的帖子?你想把我们列入政府名单吗?;) 所以我在WordPress db上尝试了这两个选项。在我的帖子中,我用标签“Perl”和“Programming”查找类别“Tech” 我在最初的select语句中添加了一个缺少的逗号后工作。它返回了3条记录。问题是,查找“post_标记”的部分实际上是一个OR选项。我的一篇文章只有一个标签,不是两个都有。另外,最好将选择的内容区分开来
我尝试了这个版本,但它一直返回一个空集。我可能会试图“摆弄”它。我误解了你的意思。我以为你想要核武器或者交易。下面应该只给你核武器和交易
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,真管用!以下是一些代码更正,以供将来参考:
- 第一个select语句在wp_term_关系tr2之后未命中昏迷
- 在同一选择状态中,必须更改以下内容:
wp_terms t3, wp_term_taxonomy tt3, wp_term_relationship
tr3
答案真是太好了。。帮了我很多忙 伟大的bcoz.,它为我提供了构建复杂查询的基本方法 对于像我这样的现成用户,有一个小小的修正:) “wp_术语_关系”将给出“不存在错误” .. 使用wp\u term\u关系,因为它是正确的表名
谢谢Eric这是一个极好的解决方案
wp_terms t3, wp_term_taxonomy tt3, wp_term_relationship
tr3