0

So I have a complex list of ACF repeaters for ingredients, but have a save hook to store them all as one comma delimited text string in a custom field to help. My current "advanced" search passes ingredient searches as a meta query and works, but non-exact matches return nothing. Like if you search "pepper and paprika" it fails, but if you search "pepper" or "paprika" there are common posts (recipes).

Is there a way to make a meta query work like a normal query a little bit more?

My meta_query:

    $args['meta_query'] = array(
        array(
            'key' => 'ingredient_list',
            'value' => $search_query,
            'compare' => 'LIKE',
        )
    );

Thank you for any help!

5
  • Does 'value' => '"' . $search_query . '"' help? Also meta queries can be named: $args['meta_query'] = [ 'query_a'=>['key' => 'ingredient_list' ... ], 'query_b'=>[ ... ] ], this can be useful when managing multiple meta queries & different combinations of and/or logic for the meta queries
    – admcfajn
    Commented Jul 7, 2022 at 21:29
  • @admcfajn The quotes actually broke it, I assume as it is already a string or something.
    – gokujou
    Commented Jul 8, 2022 at 15:54
  • Maybe look into a search plugin like Relevanssi. It'll give you much more robust functionality without needing to code everything from scratch.
    – admcfajn
    Commented Jul 9, 2022 at 4:28
  • @admcfajn I am actually using that as well, and messaged their support, but it sounds like this needs some tweaks to work right. Relevanssi is more about improving the a single search versus advanced search.
    – gokujou
    Commented Jul 12, 2022 at 19:28
  • Have you tried 'value' => '%' . $search_query . '%'? You could also take the string, use explode(' ', $search_query) to make an array of the words, remove any stop word (like "and"), then loop over them and add multiple meta queries with an or condition. Does that make sense?
    – admcfajn
    Commented Jul 12, 2022 at 20:59

2 Answers 2

0

You could break apart the search-string into an array and add each word to it's own meta-query

$search_query = 'strawberries and cream';
$search_query_array = explode(' ', $search_query); // turn search query string into array of words
$stop_words = ['and','or','that','have','with','this'];

if ( ! empty( $search_query_array ) ) {
    foreach( $search_query_array as $index ) { // remove any stop-words from the search query array
        if ( in_array($search_query_array[$index], $excluded_words) ) {
            unset( $search_query_array[$index] );
        }
    }
}

$args['meta_query'] = [
    'relation' => 'OR', // empty meta_query with or condition
];
if ( ! empty( $search_query_array ) ) {
    foreach( $search_query_array as $index ) { // remove any stop-words from the search query array
        $word = sanitize_key( $search_query_array[ $index ] );
        $args['meta_query'][ 'search_query__' . sanitize_key( $word ) ] = [ // give the meta query a handle for easy access & debugging
            'key' => 'ingredient_list',
            'value' => sanitize_key( $word ), // or '"' . sanitize_key( $word ) . '"',  or '%' . sanitize_key( $word ) . '%'
            'compare' => 'LIKE', 
        ]
    }
}

var_dump( $args ); // the final meta_query, just needs to be added to wp_query
0

I ended up having to build a custom query on the indexed custom field of ingredients for performance. Under load the meta_queries slowed way down. Not quite the answer I wanted, but it works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.