Skip Navigation

[Resolved] Using user id as a query parameter in a view of posts

This support ticket is created 2 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 2 replies, has 2 voices.

Last updated by ashiA 2 years, 9 months ago.

Author
Posts
#2324949

Hi,

I am trying to create a structure, where access to information in a post (custom post type called client) is limited to only a few set of defined users in the post. I have implemented this by adding a few custom number fields (Relationship Manager) in the post types, and this custom field stores the user ids of the people who need to have access to the information in the post.

In the content template of this Client post type, I use a conditional block to first check if the current user id is equal to the number stored in the custom field (Relationship Manager) used to indicate what users have access to this information. This part of the website is working fine. When viewing a Client post type, only users who have their user id stored under the Relationship Manager custom field have access to the content (others cannot see this content).

However when it comes to views, I am struggling to replicate this same limited access to client post types, based on whether the current user id is stored in the Relationship Manager field. In content template conditional blocks, I have an option to check 'current user id' & compare this to the number stored in the 'Relationship Manager field'.

However, when it comes to the query filter in views, I do not have an option to select 'current user id' as a query parameter. I do have an option to choose the 'Relationship Manager field' as a query parameter, but then I only have the option to compare this against a constant, URL parameter, shortcode attribute or some date options.

I wanted to understand if (a) there is a shortcode attribute I can use to get the current user id - if yes, what is this (b) is there any other way to display a view that only shows the list of clients, where the Relationship Manager field value is equal to the current user id.

Would appreciate any guidance on the same.

#2324997

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi there

Are you using the legacy editor for Views? With the legacy editor, whereby you insert a View using the wpv-view shortcode, then you can add arbitrary attributes to the shortcode, and then reference those attributes in a query filter added to the View.

See https://toolset.com/documentation/legacy-features/views-plugin/passing-arguments-to-views/

So if you have a relationship query filter where you want the value to be the current user ID, you can specify that the filter value should come from a shortcode attribute, say, "userid".

Then where you insert the View you pass the user id like so:

[wpv-view name="my-view" userid="[wpv-current-user info='id']"]
#2328857

My issue is resolved now. Thank you!