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

yoa*_*avf 7 php mysql sql wordpress plugins

这是关于在WordPress中实现的MySQL的一个非常具体的问题.

我正在尝试开发一个插件,它会显示(选择)具有特定" 标签 "且属于特定" 类别 "(多个)的帖子

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

  1. wp_posts 包含帖子列表,每个帖子都有一个"ID"
  2. wp_terms包含术语列表(包括类别和标签).每个术语都有一个TERM_ID
  3. wp_term_taxonomy 有一个带有TERM_ID的术语列表,并且每个术语都有一个分类法定义(类别或标记)
  4. wp_term_relationships 条款和职位之间有关联

我如何加入表格以获得所有带有"核" "交易" 标签的帖子,这些标签也属于"Category1"类别?

Eri*_*ric 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')
Run Code Online (Sandbox Code Playgroud)