How can I modify standard search query to include also ACF custom fields values?

Question

I have to check if word the user is looking for is also contained in a ACF custom fields. So I modify the standard query to search also into meta_query field I need:

function modifica_search( $query ) {
if ( $query->is_search() && $query->is_main_query() ) {
    $meta_query = array(
        array(
            'key' => 'contenuto',
            'value' => sanitize_text_field( $_GET['s']),
            'compare' => 'LIKE'
        )
    );

    $query->set( 'meta_query', $meta_query );
}
} 
add_action( 'pre_get_posts', 'modifica_search' );

but printng out the query (for ?s=milano)I notice that there is an AND operator (the bold one) where I need an OR :

… WHERE 1=1 AND (((nctm_posts.post_title LIKE ‘%milano%’) OR (nctm_posts.post_excerpt LIKE ‘%milano%’) OR (nctm_posts.post_content LIKE ‘%milano%’))) AND (( nctm_postmeta.meta_key = ‘contenuto’ AND nctm_postmeta.meta_value LIKE ‘%milano%’ ) …

how can I solve this?

0
, , , , Fabio 2 years 2020-08-18T14:10:35-05:00 0 Answers 55 views 0

Leave an answer

Browse
Browse