Skip Navigation

[Resolved] Add a filter to view based on user custom field

This support ticket is created 5 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.

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
- - 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00
- - - - - - -

Supporter timezone: Asia/Ho_Chi_Minh (GMT+07:00)

This topic contains 6 replies, has 2 voices.

Last updated by Greig Neilson 5 years, 2 months ago.

Assisted by: Beda.

Author
Posts
#1201146

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?

#1201240

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.

#1201321

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]
#1202779

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...

#1202944

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.

#1203436

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.

#1203483

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/

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.