0

Here's my code:

function prefix_get_users(){
   $args=array(
      'blog_id' => $GLOBALS['blog_id'],
      'search'  => 'mark'
   );
   $all_users=get_users($args);
   $arr=array();
   foreach ($all_users as $val) {
      $arr[]=$val->data->user_login;
   }
   $resp = array (
       'success'    => true,
       'data'       => json_encode($arr)
   );
   wp_send_json($resp);

 }

The code is working by getting all the user successfully but when to try to use search it's not working and give me empty array by ajax (data[])

1 Answer 1

1

get_users looks for an exact match for the value in search in the email address, URL, ID, username or display_name fields... it doesn't look in the first name field.

As you can't search the first name directly, you could instead look for people with a username or display name that starts with it by using the wildcard * in the search string, e.g.:

$args=array(
   'blog_id' => $GLOBALS['blog_id'],
   'search'  => 'mark*'   /* Note the * wildcard to match anything starting with "mark" */
);
$all_users=get_users($args);
[...]
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.