我想得到由WP用户水平的帖子。我使用get_posts()获取帖子,在参数中使用include参数。
如下所示:
$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',
);但它也给不了我任何东西。所以,请告诉我,如何才能使所有的文章在我的其他情况。
发布于 2016-02-09 12:42:11
您的问题是,如果用户未包含在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);https://stackoverflow.com/questions/35292234
复制相似问题