Created
July 31, 2024 19:16
-
-
Save rafaehlers/1c5a88b893372e018b7e62c930a619b4 to your computer and use it in GitHub Desktop.
Only performs a search if the search exactly matches the field values
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php // DO NOT COPY THIS LINE | |
add_filter( 'gravityview_fe_search_criteria', 'gk_exact_field', 10, 2 ); | |
/** | |
* @param array $search_criteria | |
* @param int $form_id | |
* | |
* @return array Modified search, if it's the current form | |
*/ | |
function gk_exact_field( $search_criteria = array(), $form_id = 0 ) { | |
// ----- Modify the IDs below ----- // | |
/** @var int $exact_match_form_id ID of the form */ | |
$exact_match_form_id = 149; | |
/** @var array $exact_match_fields ID of the fields that should be exact-match. Can also modify single inputs (as strings: "1.2") */ | |
$exact_match_fields = array( 1, 3 ); | |
// ----- Do not modify below this line ----- // | |
if( $exact_match_form_id !== intval( $form_id ) ) { | |
return $search_criteria; | |
} | |
if( !isset($search_criteria['field_filters']) ){ | |
return $search_criteria; | |
} | |
foreach ( (array) $search_criteria['field_filters'] as $key => $field_filter ) { | |
// Don't process for search mode | |
if ( ! is_numeric( $key ) ) { | |
continue; | |
} | |
// Not the right fields | |
if( ! isset( $field_filter['key'] ) || ! in_array( $field_filter['key'], $exact_match_fields ) ) { | |
continue; | |
} | |
foreach ( $exact_match_fields as $field_id ) { | |
$field = GFAPI::get_field( $form_id, $field_id ); | |
if ( ! $field ) { | |
continue; | |
} | |
if ( 'list' === $field->get_input_type() || $field->storageType === 'json' ) { | |
// @see https://gravityview.co/?p=563101 Wrap in quotes | |
$search_criteria['field_filters'][ $key ]['value'] = '"' . $field_filter['value'] . '"'; | |
$search_criteria['field_filters'][ $key ]['operator'] = 'contains'; | |
} else { | |
$search_criteria['field_filters'][ $key ]['operator'] = 'is'; | |
} | |
} | |
} | |
return $search_criteria; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment