Skip Navigation

[Closed] types termmeta comparison in conditional output

This support ticket is created 3 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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

Tagged: 

This topic contains 12 replies, has 3 voices.

Last updated by Christian Cox 3 years, 7 months ago.

Assisted by: Christian Cox.

Author
Posts
#1761669

Hi there.

I'm trying to get a conditional output with comparison but it doesn't work.

I have a custom term field named rolser-permitidos

I have this conditional

[wpv-conditional if="( '[wpv-current-user info="role"]' eq 'administrator' )"]I AM A administrator[/wpv-conditional]

and this ->

[types termmeta="roles-permitidos"][/types] 

-> output -> administrator (the category term field roles-permitidos is administrator

I'd like to get something like this but it doesn't work.

[wpv-conditional if="( '[wpv-current-user info="role"]' eq '[types termmeta="roles-permitidos"][/types]' )"]I AM A administrator[/wpv-conditional][types termmeta="roles-permitidos"][/types]

--- the current-user role is 'administrato'r and the termmeta roles-permitidos is 'administrator' but the comparison doesn't work ---

Is it possible to get this?

Thanks in advanced!

#1761837

Hi, where exactly are you trying to display this conditional? The termmeta shortcode should work in the archive for the roles-permitidos term or a View of terms where the current term is roles-permitidos. It may be helpful to turn on debugging for this conditional:

[wpv-conditional if="( '[wpv-current-user info="role"]' eq '[types termmeta="roles-permitidos"][/types]' )" debug="true"]I AM A administrator[/wpv-conditional][types termmeta="roles-permitidos"][/types]

Load this conditional on the front-end of the site and let me know the debug information that is printed to the screen.

#1762139

Hi Christian I don't understand... but yesterday term-meta was rendered. Today not. Or perhaps I did a mistake.

the debug says that roles-permitidos is empty... and it's empty. The shortcode [types termmeta="roles-permitidos"][/types] don't render anything.

####################
wpv-conditional attributes
####################
Array
(
    [if] => ( 'administrator' = '' )
    [debug] => true
)

####################
Debug information
####################
--------------------
Original expression: ( 'administrator' = '' )
--------------------
After replacing 1 general variables and comparing strings: ( 'administrator' = '' )
	Comparing administrator to 

I want to render the term meta in an POST of wordpress not in archive.

I'm logged like as administrator
category of post has administrator checked in termfield roles-permitidos

administrator = administrator -> condition true -> do anything

Is it possible or not?

Thanks for your tiime and help

#1762555

I want to render the term meta in an POST of wordpress not in archive.
Okay I think I understand. The problem is a post can have several terms applied, not only the roles-permitidos term. This means the termmeta shortcode doesn't know which term you intend to check. You could use a View of terms and a conditional in the loop, but that might not be necessary here. Since the termmeta for the roles-permitidos term will always be a static value "administrator", you could use a different conditional that does not require an extra View. You can use the WordPress function "has_term" to determine whether or not the current post has the roles-permitidos term applied. That should be enough for you to determine whether or not the post is tagged with the corresponding term. The modified conditional will look like this:

[wpv-conditional if="( ('[wpv-current-user info="role"]' eq 'administrator' ) AND (has_term( 'roles-permitidos', 'your-taxonomy-slug' ))" debug="true"]I am an administrator and this post has the roles-permitidos term applied[/wpv-conditional]

You must replace your-taxonomy-slug with the slug of the taxonomy that contains the roles-permitidos term. You can find the taxonomy slug in Toolset > Taxonomies when you edit the taxonomy.

You must also go to Toolset > Settings > Frontend Content and register has_term in "Functions inside conditional evaluations". This will allow you to use the has_term function in conditionals.

If the conditional is now working as expected, you can remove the debug="true" attribute from the conditional shortcode. Let me know if I've misunderstood what you want to achieve here.

#1764311

Hi again Christian. It doesn't work. It returns 0 (number).

I want to achieve the meta field roles-permitidos from a category of post and compare it with the role of current user (same values in roles-permitidos and roles of wp)

roles-permitidos have the following values: administrator, guest, basic
Acces roles in my wp are : administrator, guest, basic

In a post i have a conditional that compare role of current user with custom field <roles-permitidos> of a category of the current post.

If the role of the current user contains any value of the custom field of category of the current post then I show the content of the post.

If If the role of the current user doesn't contains any value of the custom field of category of the current post then I show the content of the post the I show the excerpt.

So that's the reason that i have a condicional that compares the role of current user with the value of roles-permitidos (custom field of the category of current post)

Is it possible to get this?

#1764493
Screen Shot 2020-09-02 at 2.19.25 PM.png

Okay I apologize for the confusion. I misunderstood how you have this set up. First, the system needs to know which terms are assigned to the current post. A View is the best way to handle this. You should create a View of the taxonomy that includes the roles-permitidos field. In your description, it sounds like this is the Category taxonomy. Add a Query Filter by term, where the term is set by the post where the View is shown (see the screenshot here for an example of this Query Filter). In the loop of this View, add your conditional to test the roles-permitidos termmeta value against the current User's role:

[wpv-conditional if="( '[wpv-current-user info="role"]' eq '[types termmeta="roles-permitidos"][/types]' )" debug="true"]I have the role [wpv-current-user info="role"], and the term [wpv-taxonomy-slug] has the same roles-permitidos value of [types termmeta="roles-permitidos"][/types][/wpv-conditional]

Then place this View in the Content Template assigned to this post.

Is it possible that multiple terms can be assigned to one post? If so, then this solution may produce duplicate post content. For example, if term A and term B both have the roles-permitidos field value "administrator", and both terms are applied to the same post, then the post content will be displayed twice for administrators because the View loops over all terms assigned to the current post. In this case, you need some more complex logic. What if term C has the roles-permitidos field value "basic" and terms A, B and C are all applied to the same post? What will an administrator see? What will a basic User see? Multiple terms assigned to the same post could make things very confusing here.

#1766919

Hi Christian. First of all... thanks for your time and help.

I tested your suggestion and works fine if roles-permitidos is only one, like as 'premium' However, if roles-roles permitidos are more than one, then

administrator = administrator works fine!

But if roles-permitidos are 2 or more... then it's not work

administrator = administrator,premium,basic don't work. I think that I need a contain but there is not in the conditional.

Thanks again!

#1767543
Screen Shot 2020-09-06 at 4.55.03 PM.png

If you only care about the role "administrator", then you can use a View with a Query Filter on the roles-permitidos term meta field, with a limit of 1 post, as shown in this screenshot. You can place a conditional that only tests the User role inside the wpv-items-found section of the View, outside the wpv-loop tags.

[wpv-conditional if="( '[wpv-current-user info="role"]' eq 'administrator' )" debug="true"]I have the role [wpv-current-user info="role"], and the term [wpv-taxonomy-slug] has the same roles-permitidos value of [types termmeta="roles-permitidos"][/types][/wpv-conditional]
#1767601

Hi again Christian. I'm sorry because I'm explaining this like a stone and I'm wasting your time.

I try to explain better.

I have 3 roles

administrator, basic and premium

I have a term field for taxonomy categories. The term fields roles-permitidos have 3 possible values: administrator, basic and premium

I have several categories

Category World -> with term field roles permitidos with values adminsitrator and basic
Category Music -> with term field roles permitidos with values adminsitrator and premium
Category food -> with term field roles permitidos with values basic and premium

When a premium user read a post with category music with values for roles-permitidos administrator and premium... he can read the post. A user with role basic can't read this post (category music has roles-permitidos administrator premium).

That's the reason that i need to compare value of term field roles-permitidos with role user. If the value of roles-permitidos is t he same that current role-user logged... he can read the post. If the current role logged user is different that any value of roles-permitidos of category of post.... he can't read the post.

In my confitonal i'm comparing the role of current user logged with any value of term field roles-permitidos.

If roles-permitidos has only one value... works, but if roles-permitidos has more thanone value... it doesn't work. It would be nice if condicitional can compare if role of curret user is = to any value f roles-permitidos, but it compares with all.

I don't know if my explanation is clear.

Thanks in adavnced and thanks again for your time.

#1768513

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi David,

Christian is currently on a Public Holiday today but he will be back tomorrow to continue assisting.

Thank you for the continued patience.

#1769817
Screen Shot 2020-09-08 at 4.53.47 PM.png

Okay, we must expand the solution to work for users with roles basic and premium, as well as administrator. In this case, you will configure the Query Filter I mentioned in my last comment to respond to a shortcode attribute like role. This will limit the terms in the View to those with the same roles-permitidos field value as the current logged-in User's role. You should also include a term filter set by the post where the View is shown. This will further limit the terms to only those associated with the current post. The combination of these Query Filters will have the effect that you desire. Content will only be displayed if one of the terms applied to the current post has the roles-permitidos field value identical to the current User's role slug. See the updated screenshot here for an example of both Query Filters you should apply to this View of taxonomy terms.

In the View's Content Selection panel, be sure to include each of the 3 taxonomies that contain the roles-permitidos field. Based on your description, this includes the world, food, and music taxonomies.

Next, in the loop editor, you should add the content you wish to display anywhere inside the wpv-items-found section. If you set the View's Limit to 1, only one result will ever be returned so you can add the content inside the wpv-loop tags. You may remove the "no items found" message if you'd like, or change it appropriately.

Next, insert this View in the post or the Content Template applied to the post type where you want to display the conditional content. You must pass the current logged-in User's role information into the Query Filter using a nested shortcode in the "role" attribute. Here is an example shortcode:

[wpv-view name="Your taxonomy term View" role="[wpv-current-user format='meta' info='role']"]

Replace Your taxonomy term View with the name or slug of the View of taxonomy terms you have created.

If you want to hide the content from all other User roles, including guests, you should add a conditional around that View shortcode to prevent content display for an edge case when the roles-permitidos term field of all applied terms is empty:

[wpv-conditional if="( '[wpv-current-user info="role"]' ne '')"]
  [wpv-view name="book-taxes" role="[wpv-current-user format='meta' info='role']"]
[/wpv-conditional]

This solution assumes:
1. A single User will only ever have one role.
2. Users with none of those three roles should see no content.

#1777627

I'll test this following your suggestions.

Thanks for your time Christian!

#1778729

Okay that sounds good, I'm standing by for your update.

The topic ‘[Closed] types termmeta comparison in conditional output’ is closed to new replies.