admin管理员组文章数量:1122846
I'm trying to query wordpress with multiple values but it looks like it can't take no more than 4 keywords
I'm currently using a loop and I already tried to switch to a IN operator but I need the query to work with wildcards and while REGEXP does the job, IN does not
foreach ($keywords as $value) {
$args['meta_query'] = array(
'relation' => 'OR',
array(
'key' => 'customfields',
'value' => $value,
'compare' => 'REGEXP'
)
);
}
In this case $keywords
is an array of words that I need to query to a custom field. It works with a few keywords, but it breaks with 5 or more.
I'm trying to query wordpress with multiple values but it looks like it can't take no more than 4 keywords
I'm currently using a loop and I already tried to switch to a IN operator but I need the query to work with wildcards and while REGEXP does the job, IN does not
foreach ($keywords as $value) {
$args['meta_query'] = array(
'relation' => 'OR',
array(
'key' => 'customfields',
'value' => $value,
'compare' => 'REGEXP'
)
);
}
In this case $keywords
is an array of words that I need to query to a custom field. It works with a few keywords, but it breaks with 5 or more.
2 Answers
Reset to default 0Have you tried passing an array of values to the value
argument?
$your_terms = array( 'foo', 'bar', 'baz' );
$args['meta_query'] = array(
'relation' => 'OR',
array(
'key' => 'customfields',
'value' => $your_terms,
'compare' => 'REGEXP'
)
);
$this_query = new WP_Query( $args );
In its current form, your meta_query
is overwritten in each iteration. In the final effect, you have only one custom field in meta_query
:
Depending on the operator you want to use, meta_query
argument should look as follow:
for "LIKE", "REGEXP"
if( is_array($keywords) && count($keywords) ) { $args['meta_query'] = array( 'relation' => 'OR' ); foreach( $keywords as $value ) { $args['meta_query'][] = array( 'key' => 'customfields', 'value' => $value, 'compare' => 'REGEXP', // or 'LIKE' ); } } // === result: === // $args['meta_query'] = array( // 'relation' => 'OR', // [ 'key' => 'customfields', 'value' => 'value_1', 'compare' => 'REGEXP' ], // [ 'key' => 'customfields', 'value' => 'value_2', 'compare' => 'REGEXP' ], // [ 'key' => 'customfields', 'value' => 'value_3', 'compare' => 'REGEXP' ], // );
for "IN"
if( is_array($keywords) && count($keywords) ) { $args['meta_query'] = array( 'relation' => 'OR', array( 'key' => 'customfields', 'value' => $keywords, 'compare' => 'IN', ) ); }
本文标签: custom post typesmeta query multiple values for the same key
版权声明:本文标题:custom post types - meta query multiple values for the same key 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736287031a1927794.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论