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

WordPress Trac noreply at wordpress.org
Mon Dec 17 13:09:21 UTC 2012


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

Comment (by chipbennett):

 Replying to [comment:7 DavidAnderson]:
 > Aah - gotcha. Sorry, I was confusing myself before. You are completely
 right ...
 >
 > ... up to a point (unless I'm still confused!).
 >
 > What you desire can already be done, using this syntax (which I use in
 the "Use Admin Password" plugin):
 >
 >
 > {{{
 > $desired_fields = get_users("fields[]=email");
 > }}}
 >
 >
 > So I think we just need to change Codex to give an accurate description,
 rather than re-add the same feature?

 Is that any different than:

 {{{
 $desired_fields = get_users( array( 'fields' => array( 'user_email' ) ) );
 }}}

 If so, then that ''still'' doesn't return the desired output. That returns
 an array of ''objects'', each of which contains one entity,
 `'user_email'`.

 So, when `'ID'` is passed as the single field, the output is an array of
 user IDs; but if any other field is passed as the single field, an array
 of objects is returned, since the only possible way to pass a single field
 other than ID is by passing `'fields'` as an array.

 So, user IDs can be returned as a simple array, like so:

 {{{
 $user_IDs = get_users( array( 'fields' => 'ID' ) );

 $user IDs = array(
     [0] => '1',
     [1] => '2',
     [2] => '3'
 );
 }}}

 But to get a single-field value for any other field, you can ''only'' get
 an array of objects:

 {{{
 $user_emails = get_users( array( 'fields' => array( 'user_email' ) ) );

 $user_emails = array(
     [0] => object( 'user_email' => 'email at ...' ),
     [1] => object( 'user_email' => 'email at ...' ),
     [2] => object( 'user_email' => 'email at ...' )
 );
 }}}


 The purpose of this ticket is to allow for consistent return for single-
 field values for fields other than `'ID'`; e.g. so that the following
 return is possible:

 {{{
 $user_IDs = get_users( array( 'fields' => 'email' ) );

 $user IDs = array(
     [0] => 'email at ...',
     [1] => 'email at ...',
     [2] => 'email at ...'
 );
 }}}

 Currently this return is ''not'' possible, requiring a wasteful, second
 step to loop through the array of objects to pluck the field values.

-- 
Ticket URL: <http://core.trac.wordpress.org/ticket/22921#comment:8>
WordPress Trac <http://core.trac.wordpress.org/>
WordPress blogging software


More information about the wp-trac mailing list