我有这段代码:
$categories = get_query_var('cat'); //now: $categories = "201";
$categories .= ","; //now: $categories = "201,";
$sub_cats = get_categories('parent='.$categories); //grab subcategories of this main category
foreach($sub_cats as $sub_cat){ //grab term id of subcategories
$categories .= $sub_cat->term_id;
$categories .= ",";
}
//now $categories = "201,119,394,";
$categories = substr($categories, 0, -1); //now $categories = "201,119,394";
query_posts( array( 'category__in' => array( $categories ), 'meta_key' => '_single_views', 'orderby' => 'meta_value_num', 'order' => 'DESC' , 'paged' => $paged ));
问题是它只返回201类中的帖子(所有时间只是$ category变量的第一类)。
数据库查询是:
SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts INNER JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id) INNER JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id ) WHERE 1=1 AND (
wp_term_relationships.term_taxonomy_id IN (201)
) AND wp_posts.post_type = 'post' AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'private') AND (
wp_postmeta.meta_key = '_single_views'
) GROUP BY wp_posts.ID ORDER BY wp_postmeta.meta_value+0 DESC LIMIT 0, 20
请问哪里有问题?我需要返回类别201,119,394中的所有帖子(至少在其中一个帖子中的帖子)。谢谢你的帮助。
答案 0 :(得分:0)
这很好用。
query_posts( array( 'cat' => $categories, 'meta_key' => '_single_views', 'orderby' => 'meta_value_num', 'order' => 'DESC' , 'paged' => $paged ));
答案 1 :(得分:0)
好的,建议:要么颠倒两行:
$categories .= ","; //now: $categories = "201,";
$sub_cats = get_categories('parent='.$categories);
原因:类别(201)变为201,然后在parent = $ categories中使用。由于。
,因此无法返回任何内容或使用此替代方法:
$categories = get_query_var('cat'); //now: $categories = "201";
$sub_cats = get_categories('parent='.$categories); //grab subcategories of this main category
foreach($sub_cats as $sub_cat){ //grab term id of subcategories
$categories .= ",".$sub_cat->term_id;
}
query_posts( array( 'category__in' => array( $categories ), 'meta_key' => '_single_views', 'orderby' => 'meta_value_num', 'order' => 'DESC' , 'paged' => $paged ));
P.S。类别_是否正确?连续两次下划线可能会带来更多麻烦。