I have a post type called Students. Each record has a relationship to a post type called School.
I have a custom field for my users so that I can identify them as belonging to School A or School B.
How do I write a filter for my View so that it will only return the records related to the School that the user belongs to?
I assume, "only return the records related to the School that the current logged in user belongs to", correct?
You could create a View, list Schools, and set an HTML condition in the loop checking upon the currently logged in user, its field, and then compare that to the currently displayed post.
If the post is not equal to what the user saved for the field, hide that post from the loop.
Something like this:
[wpv-conditional if="( '[types usermeta='your_user_field_slug' current_user='true'][/types]' eq 'the value you want to compare, could be wpv-post-slug ' )"]SHOW THE POST[/wpv-conditional]
There is no need for else, as if above is not true, nothing will display.
That should solve the task.
Hi Beda
Can you please tell me exactly where to place the shortcode in the loop? I have tried a few options and it just displays all of the records (including those that do not belong to the School that the User belongs to).
This is what I have at the moment:
[wpv-layout-start]
[wpv-items-found]
[wpv-conditional if="( '[types usermeta='user-school-name' current_user='true']' eq '[types field='school-short-name' item='@school-and-student.parent'][/types]' )"]
<!-- wpv-loop-start -->
<wpv-loop>
[wpv-post-body view_template="loop-item-in-restrict-access-to-students"]
</wpv-loop>
<!-- wpv-loop-end -->
[/wpv-conditional]
[/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]
Hello Beda? This is an urgent issue for me - I have several clients that need to use this system and I can't open it up to them until I can hide the data for users that shouldn't be seeing it...
1. Can you please tell me exactly where to place the shortcode in the loop?
Every item that you want to happen for each post displayed by the View must be within the wpv-loop tags.
2. Note also that [wpv-layout-start], [wpv-items-found] delimit the parts of the View that will display if you have items (outside the loop, once, but if have items). Layout, in this case, means, the "entire" part of the View loop - from items found to and inclusive no items found.
3. You are using a Loop Item template [wpv-post-body view_template="loop-item-in-restrict-access-to-students"]
so your condition should likely go either around that template or into that template, but not outside the loop as you show it in the last example.
Please accept my apologies for being late here, I was with flu after my weekend, and my colleagues could not reply here.
Hi Beda
I understand that sickness does happen, but it doesn't make sense for your tickets to go untouched for a few days. Maybe your system should add a note to the ticket when a supporter is absent.
Here's my code now:
[wpv-layout-start]
[wpv-items-found]
<!-- wpv-loop-start -->
<wpv-loop>
[wpv-conditional if="( '[types usermeta='user-school-name' current_user='true']' eq '[types field='school-short-name' item='@school-and-student.parent'][/types]' )"]
[wpv-post-body view_template="loop-item-in-restrict-access-to-students"]
[/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]
It still doesn't work - when logged in as a test user assigned to one school I can see all the schools' data.
I also tried placing the code in the "Templates for this View" panel and that doesn't work either.
Hi Beda, I found another support thread and used the solution in there and it worked. Here's the correct code
[wpv-conditional if="( '[types field='school-short-name' item='@school-and-student.parent'][/types]' eq '[types usermeta='user-school-name' current_user='true' output='raw'][/types]' )"]
Here's the support thread with the answer: https://toolset.com/forums/topic/filter-via-on-custom-user-field/