10up / ElasticPress

A fast and flexible search and query engine for WordPress.
https://elasticpress.io
GNU General Public License v2.0
1.25k stars 311 forks source link

Wrong results on acf search #1151

Closed iekmuby closed 5 years ago

iekmuby commented 6 years ago

When I'm trying to search by Advanced Custom Filed value, results contain additional data, which not relevant to my search criteria. For example, when I'm searching by ticker BGG, results contain also BDG. QNT contain KNT, BNT etc. This looks like wildcard search. My code for acf search:

function my_search_filter( $query ) {`
        if ( !is_admin() && $query->is_main_query() ) {`
                if ( $query->is_search ) {
                        $query->set( 'search_fields', array(
                                'post_title',
                                'post_content',
                                'meta' => array( 'ticker' )
                        ) );
                }
        }
}
add_action( 'pre_get_posts', 'my_search_filter', 1 );

How to solve it?

allan23 commented 5 years ago

Hi @iekmuby,

Perhaps the best approach would be to perform a meta_query instead of setting meta as a search_field.

https://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters