Skip Navigation

[Resolved] Check value exist in multiple values custom field

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

This topic contains 5 replies, has 2 voices.

Last updated by Waqar 1 year, 1 month ago.

Assisted by: Waqar.

Author
Posts
#2654203

Tell us what you are trying to do?
I have a custom multiple values field and I want check ,for conditional output, if a value exists.
For example If I have in multiple value field this 3 values: stringA, stringB, stringC Ho can check if the value stringB is IN or not?
I tried everythings but I could not find a solution
Could you help me please?

#2654403

Hi,

Thank you for contacting us and I'd be happy to assist.

We can use the 'option' attribute in the Types fields shortcode, to get the value of a particular option from the multiple 'checkboxes' field:
( ref: https://toolset.com/documentation/customizing-sites-using-php/functions/#checkboxes )

For example, to get the value of the second option from the field 'book-checkboxes', the shortcode will look like this:
( important note: the option index starts from 0 and not 1 )


[types field='book-checkboxes' option='1'][/types]

And the same shortcode can be used in a conditional statement like this:
( ref: https://toolset.com/documentation/legacy-features/views-plugin/using-shortcodes-in-conditions/ )


[wpv-conditional if="( '[types field='book-checkboxes' option='1'][/types]' eq '2' )"] 
Option 2 is checked!
[/wpv-conditional]

The text 'Option 2 is checked!' will only be printed if the second option for the field is checked.

You can use the shortcode in the same way, replacing the field slug 'book-checkboxes' and the value '2', as per your website.

regards,
Waqar

#2654463

In my case, the field is a text field with multiple values and I don't know the order because they were inserted by users. I don't understand how can check it.
I thought to use it as string comma separeted but I can't check by contain

In alternative I thought using a for each like this (where member is allineated with user)

[wpv-for-each field="wpcf-member"]
<!-- IF utente loggato è se membro del proggetto OR è autore faccio vedere-->
[wpv-conditional if="(('[types field=member][/types]' eq '[wpv-current-user info='login']')) "]
[wpv-post-body view_template="template-for-projects"]
[/wpv-conditional]
[/wpv-for-each]

This last above could be ok in some case but in other I need temporary shortcode to store temporary data with true ore false and I don't know how and if it is possible

#2655239

Thank you for sharing these details.

You can register a custom shortcode to check the existence of a specific custom field value from a field with multiple values.

For example:


add_shortcode('check_custom_field_value', 'check_custom_field_value_func');
function check_custom_field_value_func( $atts ) {

	$a = shortcode_atts( array(
		'field' => '',
		'value' => ''
	), $atts );

	$value = '0';

	$field_values = types_render_field( $a['field'], array( "separator" => "##" ) );
	if(!empty($field_values)) {
		$field_values_arr = explode('##', $field_values);
		if(in_array($a['value'], $field_values_arr)) {
			$value = '1';
		}
	}
	return $value;
}

The above code snippet can be included through either Toolset's custom code feature ( ref: https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/ ) or through the active theme's "functions.php" file.

Next, please add "check_custom_field_value" in the "Third-party shortcode arguments" section, at WP Admin -> Toolset -> Settings -> Front-end Content.

This custom shortcode can accept two parameters:

- field: slug of the field to check the target value from
- value: the target value/text to check

For example, to check if the value 'two' exists from the field with slug 'book-single-lines', the shortcode can be used, as:


[check_custom_field_value field='book-single-lines' value='two']

The shortcode will return '0' if the target value is not found and '1' if it is found.

The same shortcode can be used in a conditional statement as:


[wpv-conditional if="( '[check_custom_field_value field='book-single-lines' value='two']' eq '1' )"] 
The target option exists!
[/wpv-conditional]

Note: The custom code examples from our forum are shared to get you started in the right direction. You're welcome to adjust them as needed and for more personalized customization assistance, you can consider hiring a professional from our list of recommended contractors:
https://toolset.com/contractors/

#2655387

If I display
[types field='member' separator=', '][/types]
the output is: stakeuno, pouno, smuno, teamuno, teamdue

so when I write this code
[check_custom_field_value field='member' value='pouno']
I espect "1" but it returns "0"

Where is my error?

#2655677

I've tested the code and steps again and they work on my test website.

Important: The custom code checks for the text value in each instance of a repeating field, i.e. if each member name is saved separately, like:

- stakeuno
- pouno
- smuno
- teamuno
- teamdue

It will not work if the names are saved in the same line, in a single instance of the text field, like:

- stakeuno, pouno, smuno, teamuno, teamdue

In case the issue persists, you're welcome to share temporary admin login details, along with the link to the page where the issue can be seen.

Note: Your next reply will be private and making a complete backup copy is recommended before sharing the access details.

#2656175

Ok you're right it works

But the problem was that I can't use in coditional because I have to get the value from nick name of the current use and it creates nested shortcode issues, so I change your code tu use function instead of shortcode

This the final code of function that I registered in front end content of settings sections:

function check_custom_field_value_func( $field_id, $nick ) { 
    $value = '0';	
    $field_values = types_render_field($field_id, array( "separator" => "##" ) );
    if(!empty($field_id)) {
        $field_values_arr = explode('##', $field_values);
        if(in_array( $nick, $field_values_arr)) {
            $value = '1';
        }
    }
    return $value;
}

and I use in coditional

<!-- IF utente loggato  è se membro del proggetto OR è autore faccio vedere-->
[wpv-conditional if="( check_custom_field_value_func('member','[wpv-user field=nickname]') eq '1' )"]
  [wpv-post-body view_template="template-for-projects"]
[/wpv-conditional]