Combine multiple words in custom meta search query

Split the search query by white space and change ‘compare’ to ‘IN’ to be able to use meta values array:

<?php

$s = get_search_query();

$s_array = explode( ' ', $s ); // search query array

$args = array(
    'post_type'  = > 'product',
    'meta_query' = > array(
        'relation' => 'OR',
        array(
            'key'     => 'vbs_author',
            'value'   => $s_array, 
            'compare' => 'IN', // note the change
        ),
        array(
            'key'     => 'vbs_publisher',
            'value'   => $s_array,
            'compare' => 'IN', // note the change
        ),
    )
);

Maybe you’ll need to remove duplicates after this.

Leave a Comment