Skip Navigation

[Resolved] conditional display, user meta = taxonomy

This support ticket is created 4 years, 5 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
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10: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/Kolkata (GMT+05:30)

This topic contains 4 replies, has 3 voices.

Last updated by MatthewE391 4 years, 5 months ago.

Assisted by: Minesh.

Author
Posts
#1633343

Tell us what you are trying to do?
Hello there I'm trying to create the follow scenario in our website, users should be able to see certain info depending on wheather or not they have signed an NDA. If they haven't signed an NDA for a certain sector, they will only see basic information when visiting the custom post type. If they have been given permission, then they can see all information.

I am trying to use conditional tags, or access tags. to achieve this, but I am running into an issue where I don't think there's documentation that quite covers what i'm doing so was wondering if toolset can do it. Elements of it are covered but I'm not sure if I can combine them.

Essentially I'm trying to conditionally display to function as follows. ------

USER Has toolset field called nda-sectors. (A set of check boxes, as they can have permissions for multiple sectors) -

Custom post type (prospectus) has multiple custom fields for normal visitors and multiple fields only for logged in NDA visitors, post type also has a custom taxonomy 'sectors' (a prospectus can be in multiple sectors)

I'm trying to create a conditional tag that does the following and I'm not sure if I can.

[wpv-conditional if = "(

'[wpv-post-taxonomy type='sector' format='slug' ]' Contains ' [types usermeta='nda-sectors' current_user='true' output='raw'][/types] '

)"]

Secret information to display

[/wpv-conditional]

The possibility of multiple sector on both sides is making me fall down hard and I'm not sure if this is possible. Any help woulf be greatly appreciated.

What is the link to your site?
hidden link

but it's a very basic hot mess right now for testing.

#1633747

Hello and thank you for contacting the Toolset support.

The condition will not work because it does not get the proper input. The "Contains" function will verify if a value is included in an array(list). However, the [types usermeta='nda-sectors' current_user='true' output='raw'] will return a list instead of a single value.

You can create a custom function and register it to be used within conditional shortcodes, so you will get something like:

[wpv-conditional if = "(my_function_to_check([wpv-post-id]))"]

The function will take the ID of the post and will return true or false, whether the user should access it or not.
The function will pull the taxonomy terms from the post ID, and pull the user field values from the current user, then verify if the terms assigned to the post are all available within the current user custom field.
https://toolset.com/documentation/user-guides/views/conditional-html-output-in-views/using-custom-functions-in-conditions/

I hope this helps. Let me know if you have any questions.

#1640207
2020-05-27 11_12_08-Plumbing franchise – Franchise Resales.png
2020-05-27 11_12_15-No NDA – Franchise Resales.png
2020-05-27 11_12_23-Care Franchise – Franchise Resales.png

Thank you for you're direction, I've put this together but I have come across about 3 issue that I was hoping you might be able to help with/ have knowledge of. I'm using the following conditional statement and function but i'm quite new to wordpress and I think i'm missing something. it only seems to work on the term with the lowest ID, despite my user matching all 3 taxonomy items.

Thing 1 (probably my lack of knowledge)

[wpv-conditional if="( compare() eq '1' )"] content [/wpv-conditional]

Function-------------------------

function compare() {

$terms = get_the_terms($post->ID, 'sector');

$user = wp_get_current_user();

$userMetas = get_user_meta($user->ID, 'wpcf-nda-sectors')[0];

$key = "wpcf-fields-checkboxes-option-ed3e4a0f7498aa93af48e5e1a449d8e0-1";
if (array_key_exists($key, $userMetas)) {

$userMetas2 = $userMetas[$key];

foreach($terms as $term) {

foreach ($userMetas2 as $userMeta) {
if ($userMeta == $term->name) {
return 1;
}
}

}
}

return 0;

}
-------------------------------------------See attached images for examples

I know this is probably not under supports remit, not an official function but you might know a quick pinpoint why my function is finicky.

#1641191

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Jamal is on vacation. This is Minesh here and I will take care of this ticket. Hope this is OK.

Based on the code you shared, it seems to me the issue is that you do not added global $post so that you can get the current post ID.

For example - I've added global $post; to your function.

function compare() {
global $post;
$terms = get_the_terms($post->ID, 'sector');

$user = wp_get_current_user();

$userMetas = get_user_meta($user->ID, 'wpcf-nda-sectors')[0];

$key = "wpcf-fields-checkboxes-option-ed3e4a0f7498aa93af48e5e1a449d8e0-1";
if (array_key_exists($key, $userMetas)) {

$userMetas2 = $userMetas[$key];

foreach($terms as $term) {

foreach ($userMetas2 as $userMeta) {
if ($userMeta == $term->name) {
return 1;
}
}

}
}

return 0;

}

Do you see it works as expected?

#1642931

My issue is resolved now. Thank you!