Skip Navigation

[Resolved] User View that filters on the negative of a role

This support ticket is created 3 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 10 replies, has 2 voices.

Last updated by melodyM 3 years, 1 month ago.

Assisted by: Minesh.

Author
Posts
#2308275

We are setting up a View to display users. I see that you can set a filter to only show users with certain roles. What we want is the opposite of this. We would like to see a list of all users that DO NOT have a specific role assigned. I'm setting this up in your Legacy editor. Can you let me know if there is a way to set a conditional that will only show users that do not have a specific role assignment.

Note that I can't select all roles except for the one as the filter. The reason is because that will show users that have the other roles whether they have the specific one we're checking or not. That's not what we're wanting. We want it to show users that are not a certain role regardless of any other roles they might have.

#2308391

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

Do you meant that you want to list users where user having no role assigned?
Or
you want to search for specific role lets say "Subscriber" and list all subscriber users only?

#2308849

No, we have a role called Freelancer. We want to make a list of all users that do not have Freelancer assigned as a role.

Say we have a user that is an admin and a freelancer. If we set up a filter that shows every role except freelancer, then the user that is both admin and freelancer will show up in those results since that user meets the criteria of having the admin role. So that won't work. What we need is a conditional that checks that the freelancer role is not present regardless of any other roles.

#2310081

Minesh
Supporter

Languages: English (English )

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

To check the multiple roles, you will require to use the custom shortcode:

Please check the following related ticket:
- https://toolset.com/forums/topic/conditional-display-based-on-user-role-not-working-if-user-has-multiple-roles/#post-1575623

Then you can use that custom shortcode with [wpv-conditional] shortcode:

For example:

[wpv-conditional if="( '[roles role='freelancer']' eq '0' )"]
   
display your content
   
[/wpv-conditional]

Please do not forget to register the shortcode name roles at Toolset > Settings > Front-end Content > Custom Shrotcode or functions section.

#2310381

I'm trying to do the last step of this to register the shortcode name roles, but when I enter roles, it shows it with a red X next to it for reasons unknown. I initially named the code snippet check-user-roles and that didn't work. So then I changed it to roles and that still didn't work.

But then below that, in the Functions inside conditional evaluations area, there are also a whole bunch of Woocommerce related short codes showing in there with red X's as well under Functions registered manually.

Is the red X just something to delete the shortcode and not an error/warning and not indicative of anything being wrong? If so, then I'd recommend changing the color of that away from red. Whenever I see red in here, I'm thinking it's an error/warning.

I've added everything as I think it should be done, but the page is showing 2 users.

hidden link

It should only be showing the Toolset user account here. The admin account has Administrator, Freelancer as roles, so it shouldn't be showing that user here. Obviously I've done something incorrectly here, but I'm not sure what.

#2310999

Minesh
Supporter

Languages: English (English )

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

Can you please share admin access so I can check whats going wrong with your setup.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I have set the next reply to private which means only you and I have access to it.

#2311109

Minesh
Supporter

Languages: English (English )

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

I've I think better solution then adding the custom shortcode and conditional statement.

I've added the following filter hook to "Custom roles" snippet under "Custom Code" section offered by Toolset:
=> hidden link

function tssupp_filter_user_query($query_args, $view_settings, $view_id){
   if( $view_id == 6646){
     $user_ids  = get_users( [ 'fields'=> 'ids','role__in' => [ 'freelancer' ] ] );
     $query_args['exclude'] = $user_ids;
   }
    return $query_args;
}
add_filter('wpv_filter_user_query', 'tssupp_filter_user_query', 101, 3);

I can see its not displaying the admin user now:
- hidden link

Is this the expected output? If yes:
- you can remove the conditional statement you added within the view's loop.

More info:
- https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_user_query

#2311133

Looks good! Thanks a bunch. Did you create this cavanaughab user for testing? Just making sure.

#2311141

Minesh
Supporter

Languages: English (English )

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

No - I did not created any user on your site.

#2311439

Hmm, that's odd then. Thanks for letting me know. I guess someone found the site and had to have ordered something to get the Customer role. That's so weird. There are no orders but somehow this person got a Customer role. Oh well, not anything to do with your plugin. This one is resolved. Thanks a bunch again.

#2311441

My issue is resolved now. Thank you!