Make WordPress Core

Opened 6 years ago

Closed 6 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:

Description

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:

<?php
if(!function_exists('get_user_field_by')){

        /**
         * 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'";
                        }else{
                                $sql .= " AND $key = '$value'";
                        }
                }
                $wherecount++;
                $results = $wpdb->get_results( $sql , OBJECT );
                if(!empty($results)){
                        $results = reset($results);
                }
                return $results;
        }
        
}

Change History (1)

#1 @swissspidy
6 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.