Skip Navigation

[Resolved] Only administrators can have their posts queried in View

This support ticket is created 6 years, 2 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 5 replies, has 2 voices.

Last updated by Minesh 6 years, 2 months ago.

Assisted by: Minesh.

Author
Posts
#1110751

Hi, I have a view that lists all posts from all users of all roles.

I would like to prevent posts from administrators to be queried, unless the logged in user have administrator role.

I'm thinking of adding a conditional in the loop editor, but that just going to hide them from the list which will make the number of displayed posts per page different across pages.

Do you have a solution for this?

#1111438

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

So - you want to display only posts of currently logged-in user? OR You have a view that list all post from all author and you just want to exclude the "Administrator" posts?

#1112026

It's the second, exclude administrator posts if the logged-in user does not have an administrator role.

Here's an illustration of the posts list when an administrator is logged in
[Post title 1] by administrator_1
[Post title 2] by author_1
[Post title 3] by administrator_2
[Post title 4] by author_2
...

And here's an illustration when a user with role other than administrator is logged in
[Post title 2] by author_1
[Post title 4] by author_2
[Post title 5] by author_3
[Post title 6] by author_4
...

#1112049

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Well - what if you use the view's filter hook wpv_filter_query , so when the user is not the administrator we will fetch all non admin user IDs.

For example - Please add following code to your current theme's functions.php file

add_filter( 'wpv_filter_query', 'func_exclude_admin_role', 99, 3 );
function func_exclude_admin_role( $query, $view, $view_id ) {
global $current_user;

    if ( $view_id == 9999) {

 if(!in_array('administrator',(array)$current_user->roles)){
        // fetching all non administrator users
        $args = array('role__not_in'=>'administrator','fields'=>'ID');
        $all_non_admin_users = get_users( $args );
        if(!empty($all_non_admin_users )){
              $query['author__in'] = $all_non_admin_users ;
        }
        
    }
}
    return $query;
}

Where:
- Replace 9999 with your original view ID

#1112050

I will try this and get back to you with the result.

Thanks!

#1112219

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Ok - use the code I shared and let me know how it goes 🙂