如何调用get_posts()中的所有文章include

How to call all articles in get_posts() include

我想按 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',
           );

但它也没有给我任何帮助。所以请告诉我如何在我的 else 条件下获取所有文章。

你的问题是,如果用户不包含在你的 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);