Filtering entries by WordPress User Roles

There are two main ways to filter entries in your View based on a user's WordPress role.

Option #1: Using the Advanced Filtering Extension

Using the GravityView Advanced Filtering Extension, you can configure a filter to restrict entries in a View depending on the current user's assigned role. To set this up, you will need to utilize the Current User Role parameter.

Keep reading to see how to set this up, or watch the video tutorial below.

Suppose you have a student database (where students register on the system and select the courses they plan to study), and you only want teachers to be able to see the students registered for their course.

We can set up two condition groups that check to see if the current user's role (i.e., Physics teacher or Chemistry teacher) matches the student's course (i.e., Physics or Chemistry). This ensures that teachers only see students assigned to their course.

An advanced filter set up to restrict entries based on user role

While this is a simple example, it can be extrapolated for similar use cases. Suppose you have 5 different user roles, you would then need 5 condition groups.

Using this method, you only need to create a single View! Different entries will then display to different users based on their assigned role.

Option #2: Using the Members plugin

Another way to do this is to create one View for each user role and then use this shortcode from the Members plugin to restrict access.

[members_access role="editor"]Hide this content from everyone but editors.[/members_access]

Since you can specify the role, you can create a View for each. 

Example:

[members_access role="subscriber"][gravityview id="12"][/members_access]
[members_access role="author"][gravityview id="13"][/members_access]
[members_access role="administrator"][gravityview id="14"][/members_access]

The downside of this approach is that you will need to create multiple Views.

You can read more about the usage of the [members_access] shortcode.

Did this answer your question? Thanks for the feedback There was a problem submitting your feedback. Please try again later.

Still need help? Contact Us Contact Us