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[])

0

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);
[...]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.