Search by Multiple Values w/ Contains

This snippet extends the “contains” operator to support comparisions where a multi-value field (like a Multi Select) is the needle. Typically, the “in” operator would be used for this; however, it has the limitation of only being able to match full values rather than checking if any of the needles are contained in the haystack.

Code

Filename: gppa-multi-value-contains.php

<?php
/**
 * Gravity Perks // Populate Anything // Search by Multiple Values w/ Contains
 * https://gravitywiz.com/documentation/gravity-forms-populate-anything/
 *
 * This snippet extends the "contains" operator to support comparisions where a multi-value field (like a Multi Select)
 * is the needle. Typically, the "in" operator would be used for this; however, it has the limitation of only being able
 * to match full values rather than checking if any of the needles are contained in the haystack.
 */
add_filter( 'gppa_where_clause', function( $where_clause, $object, $table, $column, $operator, $value ) {

	static $_processing;

	if ( $_processing || ! is_array( $value ) || $operator !== 'contains' ) {
		return $where_clause;
	}

	$_processing = true;

	$clauses = array();
	foreach ( $value as $_value ) {
		$clauses[] = $object->build_where_clause( $table, $column, $operator, $_value );
	}

	$_processing = false;

	return implode( "nOR ", $clauses );
}, 10, 6 );

Leave a Reply

Your email address will not be published. Required fields are marked *

  • Trouble installing this snippet? See our troubleshooting tips.
  • Need to include code? Create a gist and link to it in your comment.
  • Reporting a bug? Provide a URL where this issue can be recreated.

By commenting, I understand that I may receive emails related to Gravity Wiz and can unsubscribe at any time.