[wp-hackers] get_users() fields array parameter

Matt Martz matt at sivel.net
Thu Dec 13 17:03:43 UTC 2012


Looking at the code for WP_User_Query::prepare_query() 

The 'fields' arg needs to be an array, to achieve what you want.

get_users( array( 'fields' => array( 'user_email' ) ) )

If it is not an array, and not the string 'all', then it queries for ID.

http://core.trac.wordpress.org/browser/tags/3.5/wp-includes/user.php#L396 

-- 
Matt Martz
matt at sivel.net


On Thursday, December 13, 2012 at 10:58 AM, Chip Bennett wrote:

> I recently tried passing 'fields' => 'user_email' in the arguments array
> for get_users(). Instead of returning user email addresses as expected, the
> call returned user IDs.
> 
> var_dump( get_users( array( 'fields' => 'user_email' ) ) );
> 
> outputs:
> 
> array(3) { [0]=> string(2) "10" [1]=> string(1) "9" [2]=> string(1) "1" }
> 
> According to the Codex, this should be an array of user
> emails<http://codex.wordpress.org/Function_Reference/get_users#Example_for_Array_of_Values_when_single_Field_specified>
> .
> 
> Is this a bug? Can anyone reproduce? (I found nothing related in Trac.) Or
> am I doing something wrong?
> 
> Note, if I make 'fields' an array, I get an array of objects that *do*
> consist of user_email values:
> 
> var_dump( get_users( array( 'fields' => array( 'user_email' ) ) ) );
> 
> outputs:
> 
> array(3) { [0]=> object(stdClass)#620 (1) { ["user_email"]=> string(22) "
> testeditor at example.com (mailto:testeditor at example.com)" } [1]=> object(stdClass)#621 (1) { ["user_email"]=>
> string(26) "testsubscriber at example.com (mailto:testsubscriber at example.com)" } [2]=> object(stdClass)#622 (1) {
> ["user_email"]=> string(20) "chip at chipbennett.net (mailto:chip at chipbennett.net)" } }
> 
> Just doing a sanity check before submitting a bug report, and/or updating
> the Codex.
> 
> Thanks,
> 
> Chip
> _______________________________________________
> wp-hackers mailing list
> wp-hackers at lists.automattic.com (mailto:wp-hackers at lists.automattic.com)
> http://lists.automattic.com/mailman/listinfo/wp-hackers
> 
> 




More information about the wp-hackers mailing list