首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何调用get_posts()中的所有文章

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

Stack Overflow用户
提问于 2016-02-09 12:34:39
回答 1查看 229关注 0票数 0

我想得到由WP用户水平的帖子。我使用get_posts()获取帖子,在参数中使用include参数。

如下所示:

代码语言:javascript
复制
$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',
           );

这里是我的完整代码:

代码语言:javascript
复制
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);

当我以订阅者、或承包商、或作者的身份登录时,它会给我提供我想要的确切文章。但是,当我注册为管理员时,它什么也没有给我(我认为这是因为我的其他条件而不能工作)。

我也试过这样做:

代码语言:javascript
复制
$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',
           );

但它也给不了我任何东西。所以,请告诉我,如何才能使所有的文章在我的其他情况。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-02-09 12:42:11

您的问题是,如果用户未包含在if块中,则将includes设置为空字符串,这意味着不包含任何帖子。

如果需要,最好只使用include键:

代码语言:javascript
复制
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);
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35292234

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档