我正在寻找一个自定义SQL语句,它将从Wordpress DB中提取匹配2个不同类别的已发布帖子。
Category 1 (Static) = "Website-1"
Category 2 (Dynamic) = "News", "Tips", "Recreation", etc.
这有点超出了我的境界,所以任何帮助都会受到高度赞赏。
这是我到目前为止所做的:
select p.* from wp_terms wt
join wp_term_taxonomy t on wt.term_id = t.term_id
join wp_term_relationships wpr on wpr.term_taxonomy_id = t.term_taxonomy_id
join wp_posts p on p.id = wpr.object_id
where
t.taxonomy = 'category' and
wt.name = 'Website-1' and
p.post_status = 'publish'
group by p.id
order by p.post_date desc
limit 10
它会拉出第一类没问题,但我需要它来匹配2个类别。
非常感谢任何见解。
谢谢!
select p.* from wp_posts p
join wp_term_relationships tr on p.id = tr.object_id
join wp_term_taxonomy tt on tt.term_taxonomy_id = tr.term_taxonomy_id
join wp_terms t on t.term_id = tt.term_id
where p.id in
(select tr2.object_id from wp_term_relationships tr2
join wp_term_taxonomy tt2 on tt2.term_taxonomy_id = tr2.term_taxonomy_id
join wp_terms t2 on t2.term_id = tt2.term_id
where
tt2.taxonomy = 'category' and
t2.name in ('Website-1') and
p.id = tr2.object_id
) and
p.post_status = 'publish' and
tt.taxonomy = 'category' and
t.name in ('News')
group by p.id
order by p.post_date desc
limit 10
我确信有更好的方法来编写此查询,因为它非常混乱,但现在可以使用。
答案 0 :(得分:0)
在分组后尝试拥有而不是其中。我会将此添加为问题/评论,但我没有足够的分数......如果切换类别的顺序会发生什么?