我希望通过WP用户级别获得帖子。我使用get_posts()来获取帖子和我使用的参数包括参数。
像这样:
$private = array(
'numberposts' => $st_cat_post_num,
'orderby' => $st_posts_order,
'category__in' => $st_sub_category->term_id,
'include' => $kbsofs_posts_arr,
'post_status' => 'publish',
);
以下是我的完整代码:
global $current_user;
$kbsofs_posts = '';
if($current_user->user_level == 0) {
$kbsofs_posts = "1,2,3 ...."; // Post IDs
} else if($current_user->user_level == 1) {
$kbsofs_posts = "10,20,30 ...."; // Post IDs
} else if($current_user->user_level == 2) {
$kbsofs_posts = "100,200,300 ...."; // Post IDs
} else {
$kbsofs_posts = '';
}
$kbsofs_posts_arr = explode(',',$kbsofs_posts);
$private = array(
'numberposts' => $st_cat_post_num,
'orderby' => $st_posts_order,
'category__in' => $st_sub_category->term_id,
'include' => $kbsofs_posts_arr,
'post_status' => 'publish',
);
//print_r($private);
$st_cat_posts = get_posts($private);
当我以订阅者或承包商或作者的身份登录时,它会向我提供我想要的确切文章。但是,当我以管理员身份进入时,它什么都没给我(我认为它不起作用,因为我的其他条件)。
我也试试这个:
$post_include = '';
if($current_user->user_level != 10 || $current_user->user_level != 9 || $current_user->user_level != 8) {
$post_include = "'include' => $kbsofs_posts_arr";
}
$private = array(
'numberposts' => $st_cat_post_num,
'orderby' => $st_posts_order,
'category__in' => $st_sub_category->term_id,
$post_include,
'post_status' => 'publish',
);
但它也没有给我什么。所以请告诉我如何在其他情况下获得所有文章。
答案 0 :(得分:2)
您的问题是,如果用户未包含在您的if块中,则将includes
设置为空字符串,这意味着没有任何帖子被包含在内。
最好只在需要时使用include
密钥:
global $current_user;
$private = array(
'numberposts' => $st_cat_post_num,
'orderby' => $st_posts_order,
'category__in' => $st_sub_category->term_id,
'post_status' => 'publish',
);
if($current_user->user_level == 0) {
$private['include'] = [1,2,3];
} else if($current_user->user_level == 1) {
$private['include'] = [10,20,30];
} else if($current_user->user_level == 2) {
$private['include'] = [100,200,300];
}
$st_cat_posts = get_posts($private);