Skip Navigation

[Resolved] Comparing a portion of a string in Conditional output

This thread is resolved. Here is a description of the problem and solution.

Problem:
A conditional output test which checks the value of a string field can only compare the entire string, using equals, not equals, or greater/less than.

How to check if the value *contains* a string?

Solution:
You would need to register a custom function or shortcode for such a test. An example is given in the reply below: https://toolset.com/forums/topic/comparing-a-portion-of-a-string-in-conditional-output/#post-1085588

Relevant Documentation:
https://toolset.com/documentation/user-guides/conditional-html-output-in-views/using-shortcodes-in-conditions/#checking-custom-shortcodes

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

Sun Mon Tue Wed Thu Fri Sat
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

Tagged: 

This topic contains 1 reply, has 2 voices.

Last updated by Nigel 6 years, 5 months ago.

Assisted by: Nigel.

Author
Posts
#1085455

Regarding conditional output: Is there a way to test just a portion of a string using conditional statements ("wpv-conditional if")?
For example if the field contains values like:
CX24, CX56, etc. and I want the conditional to compare just the first two characters of that string ("CX") is there an operator or expression to do that?

#1085588

Nigel
Supporter

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

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

Hi Briana

The only comparison operators available are equals, not equals, and greater/less than (https://toolset.com/documentation/user-guides/conditional-html-output-in-views/#comparison-operator).

So if you want a partial match comparison you'll need to write a custom function or shortcode to perform the comparison and register that for use in the wpv-conditional shortcode (see https://toolset.com/documentation/user-guides/conditional-html-output-in-views/using-shortcodes-in-conditions/#checking-custom-shortcodes).

Here's a custom shortcode that you might want to adapt to your needs (don't forget to register it):

add_shortcode( 'contains', function( $atts ){

	global $post;
	$return = -1;

	$atts = shortcode_atts( [ 'string' => '', 'field' => null ], $atts );

	if ( isset( $atts['field'] ) ) {

		$key = 'wpcf-' . $atts['field'];

		$field = get_post_meta( $post->ID, $key, true );

		$return = strpos( $field, $atts['string'] );

		if ( $return === false ) {
			$return = -1;
		}
	}
	return $return;

});

You pass a field slug for the custom field to test and a string for what you are searching for, e.g.

[wpv-conditional if="( '[contains field='arbitrary-text' string='ABC']' gte '0' )"]Contains ABC[/wpv-conditional]