Make WordPress Core

Opened 5 years ago

Closed 5 years ago

#44409 closed defect (bug) (duplicate)

get_userdata and get_user_field_by

Reported by: markuscode's profile markuscode Owned by:
Milestone: Priority: normal
Severity: normal Version:
Component: Users Keywords:
Focuses: Cc:


Hi last time I ask about the get_user_field, on the ticket #44392.
I ask not about usermeta, I ask about users table. How can get just user_email by user ID without selecting all information, just user_email.

Always I do for myself something like that:


         * Gets the user field by.
         * @param      string  $fuild  The fuild
         * @param      array   $where  The where
        function get_user_field_by($fuild = "",$where = array()){
                global $wpdb;
                $sql = "SELECT ".$fuild." FROM {$wpdb->prefix}users";
                $wherecount = 0;
                foreach ($where as $key => $value) {
                        if($wherecount == 0){
                                $sql .= " WHERE $key = '$value'";
                                $sql .= " AND $key = '$value'";
                $results = $wpdb->get_results( $sql , OBJECT );
                        $results = reset($results);
                return $results;

Change History (1)

#1 @swissspidy
5 years ago

  • Component changed from General to Users
  • Milestone Awaiting Review deleted
  • Resolution set to duplicate
  • Status changed from new to closed

Hey there

No need to create a new ticket for that, a comment on #44392 would suffice.

That being said, a function like get_user_field_by probably has no use case in core or in many plugins either.

Even if you only need the user's email address I'd highly recommend you to use get_userdata(). Fetching a user this way will try to avoid querying the database directly when the user has already been requested before and is available either in memory or in the object cache.

Note: See TracTickets for help on using tickets.