Skip Navigation

[Resolved] Displaying view that shows only items related to user role of logged user

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

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

This topic contains 7 replies, has 3 voices.

Last updated by davidH-33 2 years, 6 months ago.

Assisted by: Waqar.

Author
Posts
#2184009

Tell us what you are trying to do?
Hi, I have custom post type "filmy" that contains film items (movies) and I have products (Woocommerce) that has items with the same names as "filmy". When user buys some of the products / movies then some special user role based on product is assigned to his account (each product has special user role). This way I can easily control on all single pages which content will be available only for users that have paid for it and only for limited time. Now I need to find out how can I also filter the listing of custom post type "filmy" based on user roles that the logged user have. Or eventualy it can be listing of products instead of cpt filmy, it is simplifies the process. Is shout work simply like this - if the user is not logged then he sees all cpt "filmy" items, when the user is logged then he sees similar view but only with cpt items that has been already bought by this current user. Creating, assigning and removing the user roles is managed by Woocommerce plugin Members only. Is there any way how to do this?

Is there any documentation that you are following?
No.

Is there a similar example that we can see?
No.

What is the link to your site?
This is page with the cpt type "filmy" listing:
hidden link

#2184385

Nigel
Supporter

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

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

I'm not sure this can be achieved with Toolset, I think it likely needs a bespoke code solution.

It seems to me that there are some other requirements you are not mentioning.

You say you want users to be able to see a list of their own films (i.e. films they have bought).

You want guest users to be able to see a list of all films (so they can buy them).

But presumably, somewhere, you want logged-in users to also be able to see the films they have not bought so they have a chance to buy them (otherwise, they could only buy films when they were guest users and once they had bought one (some) they couldn't buy anymore.

Because you are managing this with roles, I was thinking you could implement this using Access post groups, which can be used to manage permissions for groups of posts (in this case each group would contain only one film post). You could grant permission to the relevant role as well as guests to see a film post. But, actually, everyone needs to be able to see the film posts at some point if they are to be able to buy them, so this isn't an option.

This is an unusual set up, and means there isn't any way to restrict the inclusion of posts in the output of a View in the way you describe, you (or a developer) will need to code a custom solution.

#2186589

Hi Nigel, sorry for delay I was trying to test some other solutions. Using plugin "Woocommerce Members only" I can hide or show any element on page based on user role - this has been tested and works fine. Limitation of this solution is that "Woocommerce Members only" works fine inside Elementor Pro but I can't set the limitations inside of Toolset View and apply the limitations for listing s with dynamic content. And my idea was that maybe if I add some custom field with the same name as the name of user role for each item (I have different user role for each item) then maybe where is some solution in Views to evaluate if the user role of current logged user contain same string as the custom field of each item - e.g. user role contains name of movie and custom field of movie contains the same name of movie. Do you think that this is possible or views can't use the user role name of current user in conditions?

#2186893

Nigel
Supporter

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

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

Screenshot 2021-10-04 at 15.03.35.png

Are you using the block editor?

In the screenshot you can see I'm adding a Conditional block where I'm testing whether the current user role is the same as the post title of the current page, but you could use this in a template and test against any of the post data, including custom fields.

(If you are not using the block editor you can do the same kind of thing with the wpv-conditional shortcode, which offers a similar dialog if you are inserting it in a text editor using the conditional button.)

#2187419

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

{ticket status updated}

#2187589

Hi Nigel, i'm not using block editor, only Elementor and Toolset Views so using wpv-conditional is fine for me. I realized that when I have multiple user roles then I need to register new function that will help me to display all user roles. So I followed this tutorial and registered new function :
https://toolset.com/forums/topic/how-to-display-user-role-in-loop-2/
New custom field named userrolefilmonline has been added (single line) and this field now contains same text as user role so I can later use it in conditional.

As for now I can already display all user roles for current user and it looks like the name name of user role and text in custom field named userrolefilmonline are same - so the only thing that I need to help with is to set up the wpv-conditional properly.

This is my shortcode for displaying all current user roles (works fine):
[get_usersrole user_id='[wpv-user field="ID"]']

And this my shortcode for displaying custom field named userrolefilmonline containing the name of user role (works fine):
[types field='userrolefilmonline']

I have tried these two versions of wpv-conditional but it's not working - can you please help me to fix the conditional?

[wpv-conditional if="( '[get_usersrole user_id='[wpv-user field='ID']']' eq '[types field='userrolefilmonline']' )"]
[wpv-conditional if="(CONTAINS(ARRAY([get_usersrole user_id='[wpv-user field='ID']']),[types field='userrolefilmonline']"]
#2187865

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Thanks for writing back.

On my test website, I was able to achieve this using the following custom shortcode:


function check_if_allowed_by_user_role_func( $atts ) {
	// Attributes
	$atts = shortcode_atts(
		array(
			'user_id' => '',
			'field_slug' => '',
		),
		$atts
	);

	// get user's roles
	$user_info = get_userdata((int)$atts['user_id']);
	$user_roles = $user_info->roles;

	// get role from the current post's custom field
	$role_from_field = types_render_field( $atts['field_slug'], array( ) );

	// check if the role from the custom field is attached with the user
	if (in_array($role_from_field, $user_roles)) {
		// return 1 if found
		return 1;
	}
	else
	{
		// return 0 if not found
		return 0;
	}
}
add_shortcode( 'check_if_allowed_by_user_role', 'check_if_allowed_by_user_role_func' );

This shortcode gets the user roles from the user whose ID is provided in attribute "user_id" and also the custom field value from the field whose slug is provided in the attribute "field_slug". And if that field value matches one of the roles from the user, the shortcode returns '1' and if not, it returns '0'.


[check_if_allowed_by_user_role user_id='[wpv-user field="ID"]' field_slug='userrolefilmonline']

You can use the same shortcode, in the conditions like this:


[wpv-conditional if="( '[check_if_allowed_by_user_role user_id='[wpv-user field='ID']' field_slug='userrolefilmonline']' eq '1' )"]
Allowed!
[/wpv-conditional]

[wpv-conditional if="( '[check_if_allowed_by_user_role user_id='[wpv-user field='ID']' field_slug='userrolefilmonline']' eq '0' )"]
Not Allowed!
[/wpv-conditional]

#2188217

This works fine. My issue is resolved now. Thank you!

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