Skip Navigation

[Resolved] View to select an associated user

This thread is resolved. Here is a description of the problem and solution.

Problem:

In my site there are two different user types I'm building pages for. Athletes and Trainers.

Athletes have a Trainer associated. I've added a usermeta field to the athletes called "trainer_id" which has the ID of the assigned trainer.

I was intending to create a view which displayed the trainer for the current logged in user, but I can't work out how as it seemed like the relationships work the wrong way.

Solution:

You can use Types shortcode to get current logged-in user's field value, for example:
[types usermeta='trainer_id' current_user='true'][/types]

Then pass it to below user view as shortcode attribute:

https://toolset.com/forums/topic/view-to-select-an-associated-user/#post-1640203

Relevant Documentation:

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

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: Asia/Hong_Kong (GMT+08:00)

This topic contains 2 replies, has 2 voices.

Last updated by Kelly 4 years, 6 months ago.

Assisted by: Luo Yang.

Author
Posts
#1640203

Hi! In my site there are two different user types I'm building pages for. Athletes and Trainers.

Athletes have a Trainer associated. I've added a usermeta field to the athletes called "trainer_id" which has the ID of the assigned trainer.

I was intending to create a view which displayed the trainer for the current logged in user, but I can't work out how as it seemed like the relationships work the wrong way.

I.e. creating a view which selects trainers, and query limited to the trainer_ id... but this is the trainer_id of the trainer, NOT the athlete!

do I need to create a view for athletes, limit the query to the logged in user id, and then do some sort of look up in the loop instead?

#1641023

Hello,

You can use Types shortcode to get current logged-in user's field value, for example:
[types usermeta='trainer_id' current_user='true'][/types]

Then pass it to below user view as shortcode attribute:
- Query trainers users
- Filter by:
Select users with id set by the View shortcode attribute "users" and with role "(trainers)"

Display above post view like this:
[wpv-view name="view-name" users="[types usermeta='trainer_id' current_user='true'][/types]"]

#1641365

Yep! That worked.