admin管理员组文章数量:1191370
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!
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!
Share Improve this question edited Jul 7, 2022 at 20:23 gokujou asked Jul 7, 2022 at 20:15 gokujougokujou 1012 bronze badges 5 |2 Answers
Reset to default 0You 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
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.
本文标签:
版权声明:本文标题:Need to search a custom field (ingredients, one long string per post), but want it to allow phrasesnon-exact matches 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738446760a2087258.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
'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'value' => '%' . $search_query . '%'
? You could also take the string, useexplode(' ', $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