Only show choices in the Search Bar that exist in entries
In the GravityView Search Bar widget, you may want to only display choices that exist in submitted entries (as opposed to showing all choices for each field).
Here's an example: Let's say you have a "Country" field in your form with a Drop Down list of all 195 countries, but the number of unique countries selected in form entries is only 15. You may wish to only display those 15 countries as choices in the search widget, instead of all 195!
⚠️ It's important to understand the implications of enabling this option. Pre-filtering choices will “show” what values have been selected for all submitted entries in a form, not just the filtered entries shown in a View. That means that even if you have filtered the entries in your View using advanced filters, the search fields will still show all the options.
GravityView 2.16 or above
If you're running GravityView 2.16 or above, you can enable the "Pre-filter choices" option in the search settings. Start by clicking on the gear icon next to the Search Bar label to open the search settings.
Now scroll down and check the box that says "Only show choices that exist in form entries".
GravityView 2.16 or below (advanced)
To limit searchable drop-down fields to only values that have been submitted, add this filter:
add_filter( 'gravityview/search/sieve_choices', '__return_true' );
If you want to limit this behavior to specific Views and specific form fields, modify this code:
// Only show used choices in entries for View ID 4 and Field ID 6 add_filter( 'gravityview/search/sieve_choices', function( $sieve, $filter, $context ) { if ( 4 === $context->view->ID && 6 === (int) $filter['field'] ) { return true; } return $sieve; }, 10, 3 );
Note: This will match both the choice label and the choice value.