【问题标题】:IF ELSE statement inside the Query array - Wordpress查询数组中的 IF ELSE 语句 - Wordpress
【发布时间】:2021-04-01 07:46:41
【问题描述】:

作为在 Buddypress 用户个人资料选项卡中显示帖子的功能的一部分,我有以下查询:

global $post, $bp;
$artist = bp_displayed_user_id();
$query_args = array(
            'post_type'   => 'article',
            'post_status' => array('publish', 'pending', 'draft'),
            'author'      => $artist,
            'tax_query'   => array(
                array(
                    'taxonomy' => 'magazine',
                    'field'    => 'slug',
                    'operator' => 'EXISTS',
                ),
            ),
);

我正在尝试定义将向访问者显示的帖子的帖子状态。因此,如果这是我自己的用户个人资料并且我是作者或者我是管理员,那么我应该会看到所有 3 个帖子状态'publish', 'pending', 'draft'

否则,如果我是用户个人资料的访问者(不是作者,也不是管理员),那么我应该只看到状态为 'publish' 的帖子。

我希望像其他一些答案所建议的那样在查询之外定义 $post_status,然后像这样将其调用到查询中:

$post_status = ( bp_is_my_profile() || current_user_can('administrator') ) ? 'post_status' => array('publish', 'pending', 'draft') : 'post_status' => 'publish' );
    
$query_args = array(
                'post_type'   => 'article',
                'post_status' => $post_status,
                'author'      => $artist,
                'tax_query'   => array(
                    array(
                        'taxonomy' => 'magazine',
                        'field'    => 'slug',
                        'operator' => 'EXISTS',
                    ),
                ),
);

但这似乎会导致致命错误。

我在这里做错了什么?

【问题讨论】:

  • 像这样更改三元运算符条件代码:$post_status = ( bp_is_my_profile() || current_user_can('administrator') ) ? array('publish', 'pending', 'draft') : array('publish');

标签: php arrays wordpress if-statement


【解决方案1】:

您的三元运算符条件不正确(发生赋值的地方),如下更正:

$post_status = ( bp_is_my_profile() || current_user_can('administrator') ) ? array('publish', 'pending', 'draft') : array('publish');

【讨论】:

  • @JoeBloggs 很高兴为您提供帮助。干杯! :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-02-11
相关资源
最近更新 更多