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

2019-09-17 11:05发布

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);

3条回答
唯我独甜
2楼-- · 2019-09-17 11:21
array(
      'key' => 'first_name',
      'value' => 'Z',
      'compare' => 'LIKE'
)
查看更多
对你真心纯属浪费
3楼-- · 2019-09-17 11:22

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 );
查看更多
爷、活的狠高调
4楼-- · 2019-09-17 11:32

Thats meta query works fine with WP_User_Query in latest WP:

        'meta_query' => array(
                               'key' => 'first_name',
                               'value' => 'Z',
                               'compare' => '>'
                             )
查看更多
登录 后发表回答