[wp-trac] [WordPress Trac] #53177: WP_User_Query doesn't not accept fields as string except ID

WordPress Trac noreply at wordpress.org
Mon May 10 09:47:09 UTC 2021


#53177: WP_User_Query doesn't not accept fields as string except ID
--------------------------+-----------------------------
 Reporter:  rilwis        |      Owner:  (none)
     Type:  defect (bug)  |     Status:  new
 Priority:  normal        |  Milestone:  Awaiting Review
Component:  Users         |    Version:  trunk
 Severity:  normal        |   Keywords:
  Focuses:  docs          |
--------------------------+-----------------------------
 The documentation says that:

   'fields'
   (string|array) Which fields to return. Single or all fields (string), or
 array of fields. Accepts 'ID', 'display_name', 'user_login',
 'user_nicename', 'user_email', 'user_url', 'user_registered'. Use 'all'
 for all fields and 'all_with_meta' to include meta fields. Default 'all'.

 https://developer.wordpress.org/reference/classes/wp_user_query/prepare_query/

 However, when passing any user field rather than 'ID', it always return
 IDs.

 Looking inside the code, I found this:


 {{{
 if ( is_array( $qv['fields'] ) ) {
         $qv['fields'] = array_unique( $qv['fields'] );

         $this->query_fields = array();
         foreach ( $qv['fields'] as $field ) {
                 $field                = 'ID' === $field ? 'ID' :
 sanitize_key( $field );
                 $this->query_fields[] = "$wpdb->users.$field";
         }
         $this->query_fields = implode( ',', $this->query_fields );
 } elseif ( 'all' === $qv['fields'] ) {
         $this->query_fields = "$wpdb->users.*";
 } else {
         $this->query_fields = "$wpdb->users.ID";
 }
 }}}

 https://core.trac.wordpress.org/browser/trunk/src/wp-includes/class-wp-
 user-query.php#L235

 which explains the issue well.

 Is that a bug or a missing feature?

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/53177>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list