Skip Navigation

[Resolved] Restricting view output depending on user role

This thread is resolved. Here is a description of the problem and solution.

Problem:
Restricting view output depending on user role

Solution:
There are multiple options you should check which will work best for you.

You can find the proposed solution, in this case, with the following reply:
https://toolset.com/forums/topic/restricting-view-output-depending-on-user-role/#post-1094601

Relevant Documentation:

This support ticket is created 6 years, 5 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 1 reply, has 2 voices.

Last updated by Minesh 6 years, 5 months ago.

Assisted by: Minesh.

Author
Posts
#1094138

Hello,

I have a number of user roles that I have created using the Ultimate Member plugin. I have set up a number of custom post types using WP Types (called profiles, scenarios and places) and set the UM Content Restriction option on some of these posts to only be visible for certain user roles.

This is working correctly and as expected on archive pages (built with archives section of Toolset), using the Hide from queries UM setting on the posts.

However, I would like to use the UM Content Restriction option within a custom view on the single-profile page that shows Scenarios Profiles Intermediary Posts.

If this can't be done using the views wizard builder I am happy to use php and a custom template instead, just would appreciate a little guidance in where to start with this kind of query.

So, in summary, I have:
3 CPTs (Profiles, Scenarios, Places)
Profiles and Scenarios are linked in a many to many relationship
I have an Ultimate Member user role restriction set on the scenario posts
On a single-profile page I have a view that lists all the Scenarios Profiles Intermediary Posts - but I only want this section to show the scenarios that are set to be visible for the currently logged in user's role.

I hope all of that makes sense and that there is a super easy solution to achieve what I'm after! 🙂

Many thanks in advance,
Adam

#1094601

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

Well - there are couple of options you should check which will work best for you.

Option 1:
1) Create a view that lists users of role
2) child view list posts of custom post types, filter with:
Select posts with the author set by the parent View.

More info:
Filtering Views Query by Author
https://toolset.com/documentation/user-guides/filtering-views-query-by-author/

Option 2:
You can use Toolset Access shortcode - you need to install Access plugin:

[toolset_access role="administrator,editor" operator="allow"] 

This content will be ONLY visible to administrator and editor role 

[/toolset_access]

=> https://toolset.com/documentation/user-guides/access-control-texts-inside-page-content/

Option 3:
- You can use [wpv-conditional] shortcode inside your view to check the current user role.

[wpv-conditional if="( '[wpv-current-user info="role"]' eq 'administrator' )"]
visible only to administrator
[/wpv-conditional]

=> https://toolset.com/documentation/user-guides/views-shortcodes/#wpv-current-user
=> https://toolset.com/documentation/user-guides/conditional-html-output-in-views/

Option 4:
- Use view's filter hook wpv_filter_query

add_filter( 'wpv_filter_query', 'func_filter_by_specific_role', 10, 3 );
function func_filter_by_specific_role( $query, $view, $view_id ) {
 $current_user = wp_get_current_user();

    if ( $view_id == 99999) {

      $roles = ( array ) $current_user ->roles;

        $users = array();

        foreach($roles as $role){
            $args = array(
                'role' => $role,
                'fields' => 'id'
            );
            $tmp = get_users($args);
            $users = array_merge($users, $tmp);
        }
        if(empty($users)){
            $users = array(0);
        }
        $query['author__in'] = $users;
    }
    return $query;
}

Where:
- Replace 99999 with your original view ID.

More info:
=> https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query