首页 > 解决方案 > 我需要从 3 个分类法(个人资料类别、县、部门)中获取帖子数据

问题描述

下面的查询正在使用一种分类法

SELECT * 
FROM bb_posts AS p 
LEFT JOIN bb_term_relationships AS r ON (p.ID = r.object_id) 
INNER JOIN bb_term_taxonomy AS x ON (r.term_taxonomy_id = x.term_taxonomy_id) 
INNER JOIN bb_terms AS t ON (r.term_taxonomy_id = t.term_id) 
WHERE p.post_type IN ('post', 'profile') 
AND p.post_status = 'publish' 
AND x.taxonomy = 'profile-category' 
AND t.term_id = '577' 
AND x.taxonomy = 'county' 
AND t.term_id = '804' 
AND x.taxonomy = 'Sector' 
AND t.term_id = '830' 
ORDER BY p.post_date DESC

如何从 3 个分类法中获取帖子数据?

标签: mysqlwordpress

解决方案


这是一种可能的解决方案

SELECT * FROM bb_posts 左连接 bb_term_relationships ON (bb_posts.ID = bb_term_relationships.object_id) 左连接 bb_term_relationships AS tt1 ON (bb_posts.ID = tt1.object_id) 左连接 bb_term_relationships AS tt2 ON (bb_posts.ID = tt2.postmeta_id) 内连接 bb_posts.ID = tt1.object_id开启(bb_posts.ID = bb_postmeta.post_id)其中 1=1
AND ( bb_term_relationships.term_taxonomy_id IN (689) AND tt1.term_taxonomy_id IN (798) AND tt2.term_taxonomy_id IN (828) ) AND ( ( bb_postmeta.meta_key = 'sector' AND bb_postmeta.meta_value LIKE '%Wheelchair%' ) ) AND bb_posts .post_type IN ('allusers', 'profile') AND (bb_posts.post_status = 'publish' OR bb_posts.post_status = 'acf-disabled' OR bb_posts.post_author = 1 AND bb_posts.post_status = 'private') 按 bb_posts 分组。 ID ORDER BY bb_posts.ID DESC LIMIT 0, 12


推荐阅读