0

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 Answers3

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 );
doublesharp
  • 26,888
  • 6
  • 52
  • 73
0

Thats meta query works fine with WP_User_Query in latest WP:

        'meta_query' => array(
                               'key' => 'first_name',
                               'value' => 'Z',
                               'compare' => '>'
                             )
r0b3rt0
  • 166
  • 1
  • 6
-1
array(
      'key' => 'first_name',
      'value' => 'Z',
      'compare' => 'LIKE'
)
VikingBlooded
  • 884
  • 1
  • 6
  • 17