jeudi 1 avril 2021

IF ELSE statement inside the Query array - Wordpress

I've got the following query as a part of the function that shows posts in Buddypress user profile tab:

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

I am trying to define the post status of the posts that will be showing to visitors. So if this is my own user profile and I am the author OR I am an administrator, then I should see all 3 post statuses 'publish', 'pending', 'draft'.

Else if I am a visitor to the user profile (not author, not admin), then I should only see posts that have a status 'publish'.

I was hoping to do define the $post_status outside the query like some of the other answers suggest and then call it to the query like this:

$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',
                    ),
                ),
);

But that seems to be resulting in a fatal error.

What am I doing wrong here?

Aucun commentaire:

Enregistrer un commentaire