在wordpress中,我怎样才能获得同时归为三类的帖子ID?
示例:
object_id term_taxonomy_id
-------------------------------
6913 65
7000 80
7500 65
2500 20
6913 67
6913 70
5555 65
5555 67
6913 66
所以同时有65,67和70条的帖子是6913.
答案 0 :(得分:0)
使用查询,您可以获得具有这三个类别的帖子
SELECT * FROM `wp_posts` p
LEFT JOIN `wp_term_relationships` tr ON (p.ID = tr.object_id)
INNER JOIN wp_term_taxonomy tt ON (tt.term_taxonomy_id = tr.term_taxonomy_id )
WHERE p.post_status='published' AND tt.term_id IN( 65,67,70)
HAVING COUNT(tr.object_id) =3
以上查询将返回所有包含这三个类别的帖子65,67,70
您可以使用wordpress wpdb
类来运行查询
global $wpdb;
$posts=$wpdb->get_results("SELECT * FROM `wp_posts` p
LEFT JOIN `wp_term_relationships` tr ON (p.ID = tr.object_id)
INNER JOIN wp_term_taxonomy tt ON (tt.term_taxonomy_id = tr.term_taxonomy_id )
WHERE p.post_status='published' AND tt.term_id IN( 65,67,70)
HAVING COUNT(tr.object_id) =3");