4

I have to get all the users whose first name starts with the letter "A". 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 "A"?

I used the function below

         <?php $users = get_users(array('role' => 'user_role', 
                                        'meta_key' => 'first_name',
                                        'meta_value' => 'A*',
                                        'meta_compare' => 'LIKE'));

I have found no results. Is there a mistake here?

Dancrumb
  • 26,597
  • 10
  • 74
  • 130
Ranjit
  • 1,684
  • 9
  • 29
  • 61

3 Answers3

6

In order to use LIKE, you need to use a meta_query

$args = array(
    'role' => 'user_role', 
    'meta_query' => array(
        array(
            'key' => 'first_name',
            'value' => array( 'A', 'B'),
            'compare' => 'BETWEEN'
        )
    )
);
$users= get_users($args);
Dancrumb
  • 26,597
  • 10
  • 74
  • 130
  • I want the name whose first letter is start from "A" – Ranjit Feb 06 '13 at 05:32
  • Yup... 'LIKE' automatically puts in `%` wildcards at the start and end of the `value`... try the new version. If it works, don't forget to upvote and accept (or... at least... accept) – Dancrumb Feb 06 '13 at 17:33
  • what about if I just want % wildcard at the beggining or at the end but no in both? how to solve that?. thanks – user2580401 Mar 09 '17 at 18:59
2

For those still struggling with this, to use the % operator, try the code below:

$args = array(
    'role' => 'user_role', 
    'meta_key' => 'first_name',
    'meta_query' => array(
        array(
            'key' => 'first_name',
            'value' => '^'.$search_term.'.*',
            'compare' => 'REGEXP'
        )
    )
);

This code will return the 'first_name' that starts with the letter you are querying, followed by anything.

This is explained on the codex here: https://codex.wordpress.org/Class_Reference/WP_User_Query#Custom_Field_Parameters

Note 2: with 'LIKE' the value parameter is change to '%%'. So the string is searched anywhere in the custom field value. If value parameter contain a '%' it is escaped. So it bans to search a string beginning by some characters, for example... To construct this query you must use 'REGEXP' with a regular expression in value parameter (ex : '^n.*' matches all values that beginning by "n" or "N").

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Wollivan
  • 51
  • 9
1

As has been mentioned, using LIKE means it will match any value that contains the letter, regardless if the letter is at the start or not. So using LIKE probably won't get you what you want.

The approach in WordPress seems to be using the "BETWEEN" comparison, so that it will match any value where the word is between one letter and the subsequent letter.

Per the tutorial at http://www.essentialwp.com/tag/meta_query/, you can get the character you want the word(s) to start with, and then use PHP functionality to find the subsequent character, which allows you to filter the results correctly.

// Get the value for surname and attach it to a variable
$letter = $_GET['surname'];]

// We need to establish the letter after the first letter from $letter, so in this case 'D'
$letterAfter = chr(ord($letter)+1);

// Craft a custom wp_query call
$args = array(
    'post_type' => 'profiles',  // use this if you are using a custom post type
    'order' => 'ASC',
    'meta_query' => array(
        array(
            'key' => 'surname', // name of your custom field
            'value' => array( $letter, $letterAfter ),
            'orderby' => 'meta_value',
            'compare' => 'BETWEEN',
            'order' => 'ASC'
        )
    )
 );
$wp_query = new WP_Query( $args );

// then run your loop....

You'll likely need to handle the letter Z differently, see Get the users by first name start with letter “Z”

You might also be interested in Most efficient way to get next letter in the alphabet using PHP

Community
  • 1
  • 1
pnairn
  • 1,737
  • 3
  • 17
  • 24