Get the users by first name start with letter “Z”

2019-09-17 11:15发布

问题:

I have to get all the users whose first name starts with the letter "Z". I get the user information by using the get_users() function. How can i use the argument query to get the users whose first name letter starts with "Z"?

I used the function below that show me the names that starts with "V", "W", "X", "Y", but not "Z"

$args = array(
            'role' => 'subscriber',
            'meta_query' => array(
                array(
                    'key' => 'first_name',
                    'value' => array( 'V', 'Z'),
                    'compare' => 'BETWEEN'
                )
            )
        );
        $users= get_users($args);

回答1:

Use the "greater than or equals" operator >= on the meta_value to return results where first_name starts with "V" through "Z", inclusive.

$args = array(
    'role' => 'subscriber',
    'meta_query' => array(
        array(
            'key' => 'first_name',
            'value' => 'V',
            'compare' => '>='
        )
    )
);
$users = get_users( $args );


回答2:

Thats meta query works fine with WP_User_Query in latest WP:

        'meta_query' => array(
                               'key' => 'first_name',
                               'value' => 'Z',
                               'compare' => '>'
                             )


回答3:

array(
      'key' => 'first_name',
      'value' => 'Z',
      'compare' => 'LIKE'
)