Skip Navigation

[Resolved] Only allow users to view / edit their own posts, except for certain user roles

This support ticket is created 6 years, 9 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.

This topic contains 3 replies, has 3 voices.

Last updated by richardG-4 6 years, 9 months ago.

Assigned support staff: Tom.

Author
Posts
#353784

I am trying to do something like this:

https://toolset.com/forums/topic/limit-search-to-custom-post-type-posts-created-by-logged-in-user/

However, I would like to allow certain user roles to view all posts, such as admins, but perhaps another intermediate user role as well.

#353824

Tom

Thanks for contacting us!

What you want to achieve is possible with conditional statements in the views loop.

Here you check if Current user === post author:

[wpv-conditional if="('[wpv-current-user info='id']' eq '[wpv-post-author format='meta' meta='ID']')"]
your content to display if condition is true
[/wpv-conditional]

To check for user role you need:

[wpv-conditional if="( '[wpv-current-user info='role']' eq 'your_role' )"]
your stuff here
[/wpv-conditional]

To do this use the "conditional" button together with Fields and Views button.

You have to add one conditional statement for each role and edit "eq" or "ne" (not equal) if you want to hide/show your content.

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

If I misunderstood, please provide more detail what you want to achieve exactly.

For further information or if you have any questions related to this ticket
please do not hesitate to contact us.

#353835

Thanks. I think that's exactly what I was looking for.

#1402105

Here is how I set it up so administrators and the author could both view content:

[wpv-conditional if="('[wpv-current-user info='id']' eq '[wpv-post-author format='meta' meta='ID']') OR ('[wpv-current-user info='role']' eq 'administrator')"]