admin管理员组文章数量:1287636
I don't see that it is possible when I look at the WordPress documentation but I wanted to confirm. I want to check multiple meta values against one meta key eg:
$user_query = new WP_User_Query( array( 'meta_key' => 'user_charname', 'meta_value' => 'squarepants', 'orderby' => 'meta_value_num', 'order' => 'DESC' ));
I was hoping I could just use 'meta_value' => array('value1', 'value2')
but that does not work.
I have tried this as pointed out by Jacob (modified) but it gives me what appears to be all results and isn't working as intended.
$args = array(
'meta_query' => array(
array(
'key' => 'user_charname',
'value' => array('spongebob', 'mickey'),
'compare' => '='
)
)
);
$user_query = new WP_User_Query( $args );
I don't see that it is possible when I look at the WordPress documentation but I wanted to confirm. I want to check multiple meta values against one meta key eg:
$user_query = new WP_User_Query( array( 'meta_key' => 'user_charname', 'meta_value' => 'squarepants', 'orderby' => 'meta_value_num', 'order' => 'DESC' ));
I was hoping I could just use 'meta_value' => array('value1', 'value2')
but that does not work.
I have tried this as pointed out by Jacob (modified) but it gives me what appears to be all results and isn't working as intended.
$args = array(
'meta_query' => array(
array(
'key' => 'user_charname',
'value' => array('spongebob', 'mickey'),
'compare' => '='
)
)
);
$user_query = new WP_User_Query( $args );
Share
Improve this question
edited Sep 21, 2021 at 11:47
user8463989
asked Sep 21, 2021 at 11:25
user8463989user8463989
5931 gold badge8 silver badges24 bronze badges
2
- 1 Have you read developer.wordpress/reference/classes/wp_user_query/…? – Jacob Peattie Commented Sep 21, 2021 at 11:33
- @JacobPeattie, thank you for that. I took a look and did try to use it but perhaps my implementation is incorrect as it doesn't work as intended. – user8463989 Commented Sep 21, 2021 at 11:48
1 Answer
Reset to default 1If you just wanted to know if it's possible, then yes it is possible.
But the problem as I could see it from your edited question, is that you set the compare
to =
which should instead be IN
.
However, you didn't actually have to set it because when you supply an array of values, the operator will default to IN
.
So the correct code would be:
'meta_query' => array(
array(
'key' => 'user_charname',
'value' => array('spongebob', 'mickey'),
'compare' => 'IN' // use IN
)
)
本文标签: wp queryarray of meta values using WPUserQuery
版权声明:本文标题:wp query - array of meta values using WP_User_Query 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741309275a2371556.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论