I’ve been trying to solve how to get my results to display alphabetically by first_name from the usermeta table since your last post minus a few hours of shut eye and I feel I’m pretty well stuck. I realize this is getting beyond the scope of Register Plus and appreciate your consideration of help.
I located the functions you use in your provided code and tried to modify them to include an ORDER BY $wpdb->first_name ASC (also tried some other methods). I copied these functions below for reference.
One of the problems is the get_row function returns the results I need, however it doesn’t seem that you can use ORDER BY in the statement because it is looping through selecting single rows one at a time.
I think what it comes down to is if each key in the usermeta table was stored in it’s own field, I’d be able to easily write a select statement that alphabetizes ASC, but because I need to alphabetize by a key in the meta_key field with values stored in the meta_value field, I’m having trouble. Do you have a solution for this?
(WP functions used in above code snippet listed below for reference)
get_users_of_blog()
Defined at: * /wp-includes/user.php -> line 218
/**
* Get users for the blog.
*
* For setups that use the multi-blog feature. Can be used outside of the
* multi-blog feature.
*
* @since 2.2.0
* @uses $wpdb WordPress database object for queries
* @uses $blog_id The Blog id of the blog for those that use more than one blog
*
* @param int $id Blog ID.
* @return array List of users that are part of that Blog ID
*/
function get_users_of_blog( $id = '' ) {
global $wpdb, $blog_id;
if ( empty($id) )
$id = (int) $blog_id;
$users = $wpdb->get_results( "SELECT user_id, user_login, display_name, user_email, meta_value FROM $wpdb->users, $wpdb->usermeta WHERE " . $wpdb->users . ".ID = " . $wpdb->usermeta . ".user_id AND meta_key = '" . $wpdb->prefix . "capabilities' ORDER BY {$wpdb->usermeta}.user_id" );
return $users;
}
—————————————–
get_userdata()
Defined at: * /wp-includes/pluggable.php -> line 113
/**
* Retrieve user info by user ID.
*
* @since 0.71
*
* @param int $user_id User ID
* @return bool|object False on failure, User DB row object
*/
function get_userdata( $user_id ) {
global $wpdb;
$user_id = absint($user_id);
if ( $user_id == 0 )
return false;
$user = wp_cache_get($user_id, 'users');
if ( $user )
return $user;
if ( !$user = $wpdb->get_row($wpdb->prepare("SELECT * FROM $wpdb->users WHERE ID = %d LIMIT 1 ORDER BY $wpdb->business_name ASC", $user_id)) )
return false;
_fill_user($user);
return $user;
}
endif;
if ( !function_exists('update_user_cache') ) :
/**
* Updates a users cache when overridden by a plugin.
*
* Core function does nothing.
*
* @since 1.5
*
* @return bool Only returns true
*/
function update_user_cache() {
return true;
}
endif;
————————————————–
get_row()
Defined at: * /wp-includes/wp-db.php -> line 723
/**
* Retrieve one row from the database.
*
* @since 0.71
*
* @param string $query SQL query
* @param string $output ARRAY_A | ARRAY_N | OBJECT
* @param int $y Row num to return
* @return mixed Database query results
*/
function get_row($query = null, $output = OBJECT, $y = 0) {
$this->func_call = "\$db->get_row(\"$query\",$output,$y)";
if ( $query )
$this->query($query);
else
return null;
if ( !isset($this->last_result[$y]) )
return null;
if ( $output == OBJECT ) {
return $this->last_result[$y] ? $this->last_result[$y] : null;
} elseif ( $output == ARRAY_A ) {
return $this->last_result[$y] ? get_object_vars($this->last_result[$y]) : null;
} elseif ( $output == ARRAY_N ) {
return $this->last_result[$y] ? array_values(get_object_vars($this->last_result[$y])) : null;
} else {
$this->print_error(/*WP_I18N_DB_GETROW_ERROR*/" \$db->get_row(string query, output type, int offset) -- Output type must be one of: OBJECT, ARRAY_A, ARRAY_N"/*/WP_I18N_DB_GETROW_ERROR*/);
}
}