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
Add a comment  | 

1 Answer 1

Reset to default 1

If 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