Home › Toolset Professional Support › [Resolved] Access hide view items
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.
No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.
Sun | Mon | Tue | Wed | Thu | Fri | Sat |
---|---|---|---|---|---|---|
9:00 – 13:00 | 9:00 – 13:00 | 9:00 – 13:00 | 9:00 – 13:00 | - | - | 9: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: Africa/Casablanca (GMT+01:00)
Tagged: Access plugin, Views plugin
This topic contains 3 replies, has 2 voices.
Last updated by Jamal 4 years, 7 months ago.
Assisted by: Jamal.
I have a custom post type called Cards. I have created a view that shows all Card posts in a grid. I have created a custom taxonomy for the cards. What I am trying to do is hide some of the cards in the grid based on category using Access. Only users with the correct role can view the cards. I.e for everyone else they will simply not appear in the grid.
I dont seem to be able to do this using Post Groups as I can only search on post name not category or block the whole custom post type which i dont want to do. I was just wondering if there was a way of doing this (I feel like there should be I just can't figure it out).
So...
Subscribers can see all posts excepts those which have category C assigned to them.
Users with the Card role can see all posts
Thanks!
Hello and thank you for contacting the Toolset support.
Toolset Access does not check the permissions on the views queries. You will need to use the conditions on the loop to show/hide an item. Check this article about Conditional Output in Views.
https://toolset.com/documentation/user-guides/views/conditional-html-output-in-views/#connecting-conditions
Check also the "wpv-current-user" shortcode which will allow you to get the user role
https://toolset.com/documentation/user-guides/views/views-shortcodes/#vf-153354
The following code will hide any post assigned to the category "food" for users that are editors:
[wpv-conditional if="( CONTAINS(#(category),'food') AND "[wpv-current-user info="lastname"] == 'editor' " )"] View code here [/wpv-conditional]
You may want to use the "toolset_access" shortcode too. Check this article https://toolset.com/documentation/user-guides/access-control/access-control-texts-inside-page-content/
You may also want to handle this with custom code and excluding the posts assigned to the taxonomy term from the view query. Check this ticket that tries to do it. You will need to adapt code for checking the user role.
https://toolset.com/forums/topic/excluding-a-taxonomy-from-a-view-using-a-filter-hook/
I hope this helps. If you found any troubles, please allow me temporary access to take a closer look at your condition
Hi Jamal
This is really helpful thanks. I believe it will be the user wpv-current-user shortcode that you have provided that will do the trick but I may need to amend it a little. I hope you can help.
1) Does this conditional code go around the view template or the loop itself.
2) I am using a tag not a category the tag is called POL.
3) Is it possible to use a list of user emails rather than a role (this is an internal intranet and all usernames are the users email) that can see the post (basically only 5 or 6 users should be able to view Cards tagged as POL.
So the code I am looking for would be - HIDE any post assigned to the tag "POL" for ALL users EXCEPT test@test.com, test1@test.com, test2@test.com.
Or maybe better ONLY show Cards tagged as POL to test@test.com, test1@test.com, test2@test.com
I hope that makes sense!!
Hi and thanks.
1) Conditional code goes inside the <wpv-loop> tags, or inside the loop content template if used.
2) Categories and Tags are both taxonomies, just use the tags taxonomy slug instead of the category slug ( CONTAINS(#(tags),'pol').
3) Of course, you can combine expressions inside the condition with an operator like AND, OR, NOT and you can also use the evaluate attribute to negate the condition.
Check this example code, I hope it will answer all the three questions:
[wpv-layout-start] [wpv-items-found] <!-- wpv-loop-start --> <wpv-loop> [wpv-conditional if="( CONTAINS(#(tag),'pol') )"] [wpv-conditional if="( ( '[wpv-current-user info='email']' eq 'test@test.com' ) OR ( '[wpv-current-user info='email']' eq 'test1@test.com' ) )" ] [wpv-post-body view_template="my-loop-content-template"] [/wpv-conditional] [/wpv-conditional] [wpv-conditional if="( CONTAINS(#(tag),'pol') )" evaluate="false"] [wpv-post-body view_template="my-loop-content-template"] [/wpv-conditional] </wpv-loop> <!-- wpv-loop-end --> [/wpv-items-found] [wpv-no-items-found] <strong>[wpml-string context="wpv-views"]No items found[/wpml-string]</strong> [/wpv-no-items-found] [wpv-layout-end]
Please note the following:
- This view is using a content template "my-loop-content-template".
- When the post has the tag pol, we verify if the current user is from the selected users using the operation OR. This condition was built only with a single quote(') instead of a double quote(").
- We used the attribute (evaluate="false") for the posts that are not assigned to pol tag. We may also use the operator NOT instead of this attribute. Seel line 11.
I hope this answers your questions, let me know if you have any doubts.