我有一个帖子,它有多个类别,我怎么能通过SQL查询得到这篇文章的所有类别?非常感谢。
答案 0 :(得分:5)
您可以使用此SQL获取帖子类别:
SELECT wt.* FROM wp_posts p
INNER JOIN wp_term_relationships r ON r.object_id=p.ID
INNER JOIN wp_term_taxonomy t ON t.term_taxonomy_id = r.term_taxonomy_id
INNER JOIN wp_terms wt on wt.term_id = t.term_id
WHERE p.ID=1 AND t.taxonomy="category"
答案 1 :(得分:1)
检查此代码 -
$post_categories = wp_get_post_categories( $post_id );
$cats = array();
foreach($post_categories as $c){
$cat = get_category( $c );
$cats[] = array( 'name' => $cat->name, 'slug' => $cat->slug );
}
参考:http://codex.wordpress.org/Function_Reference/wp_get_post_categories -
答案 2 :(得分:0)
试
select c.*
from wp_categories c
inner join wp_post2cat pc on pc.category_id = c.cat_ID
inner join wp_posts p on pc.post_id = p.id
where p.id = 1