Skip to content

Instantly share code, notes, and snippets.

@zackkatz
Forked from luistinygod/gist:9dcb82e1eed60ee7e4b1
Last active December 14, 2018 10:53
Show Gist options
  • Save zackkatz/66e9fb2147a9eb1a2f2e to your computer and use it in GitHub Desktop.
Save zackkatz/66e9fb2147a9eb1a2f2e to your computer and use it in GitHub Desktop.
For Tim
<?php
/**
* Place this in your theme's functions.php
* Please adapt the form_id and the field key to your own case.
* This is intended to force GravityView to search for the exact value instead of be more relaxed (default search uses operator LIKE)
*
*/
add_filter( 'gravityview_fe_search_criteria', 'my_gv_exact_search', 20, 2 );
function my_gv_exact_search( $search_criteria, $form_id = '' ) {
if( '1' != $form_id ) { return $search_criteria; }
if( empty( $search_criteria['field_filters'] ) || !is_array( $search_criteria['field_filters'] ) ) { return $search_criteria; }
foreach( $search_criteria['field_filters'] as $k => $filter ) {
if( !empty( $filter['key'] ) && '6' == $filter['key'] ) {
$search_criteria['field_filters'][ $k ]['operator'] = 'is';
break;
}
}
return $search_criteria;
}
@soulseekah
Copy link

soulseekah commented Dec 14, 2018

This should be redone using the gravityview_search_operator filter instead.

add_filter( 'gravityview_search_operator', 'my_gv_exact_search', 20, 2 );
function my_gv_exact_search( $operator, $field ) {
    if ( $field['key'] == '6' && $field['form_id'] == '1' ) {
        return 'is';
    }
    return $operator;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment