[wp-trac] [WordPress Trac] #22921: Allow get_users() to return array of values via 'fields' parameter

WordPress Trac noreply at wordpress.org
Wed Apr 14 13:24:16 UTC 2021


#22921: Allow get_users() to return array of values via 'fields' parameter
-------------------------------------------------+-------------------------
 Reporter:  chipbennett                          |       Owner:  (none)
     Type:  enhancement                          |      Status:  new
 Priority:  normal                               |   Milestone:
Component:  Users                                |     Version:  3.5
 Severity:  normal                               |  Resolution:
 Keywords:  has-patch needs-testing dev-         |     Focuses:
  feedback needs-refresh                         |
-------------------------------------------------+-------------------------

Comment (by lucasbustamante):

 +1, I just ran this query and got an array of user IDs, which is counter-
 intuitive:

 {{{#!php
 <?php
 $adminEmails = get_users([
     'role' => 'administrator',
     'fields' => 'user_email',
 ]);

 // Result: [ '1' ]
 // Where `1` is the ID of the admin user.
 }}}

 If instead I query:

 {{{#!php
 <?php
 $adminEmails = get_users([
     'role' => 'administrator',
     'fields' => [
         'user_email'
     ],
 ]);

 // I get an array of stdClass objects:

 // Result: [ (object) [ 'user_email' => 'dev at wp-foo.com' ]

 // So to get the e-mail I need to:
 // $adminEmails[0]->user_email === 'dev at wp-foo.com';
 }}}

 Which is essentially the same problem as running `get_post_meta()` with
 the `single` true or false. I expect a single result, but instead I get a
 collection of results that I need to parse afterwards.

 So my suggestion is that:

 {{{#!php
 <?php
 $adminEmails = get_users([
     'role' => 'administrator',
     'fields' => 'user_email',
 ]);

 // Would return an array of single results: ['dev at wp-foo.com']
 }}}


 While keeping the query by array the same as it is today, to keep
 backwards compatibility. The only change is that when asking for a single
 result like `user_email` it would return the actual email instead of a
 list of IDs.

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/22921#comment:18>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list