Fetch only the required field in wp_dropdown_users(). See #14572

git-svn-id: http://svn.automattic.com/wordpress/trunk@16974 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
scribu 2010-12-15 23:56:53 +00:00
parent 932ef80a9e
commit 0a68fc29d2
1 changed files with 28 additions and 9 deletions

View File

@ -351,7 +351,8 @@ class WP_User_Query {
*/ */
var $total_users = 0; var $total_users = 0;
// SQL pieces // SQL clauses
var $query_fields;
var $query_from; var $query_from;
var $query_where; var $query_where;
var $query_orderby; var $query_orderby;
@ -406,8 +407,19 @@ class WP_User_Query {
$qv = &$this->query_vars; $qv = &$this->query_vars;
$this->query_from = " FROM $wpdb->users"; if ( is_array( $qv['fields'] ) ) {
$this->query_where = " WHERE 1=1"; $qv['fields'] = array_unique( $qv['fields'] );
$this->query_fields = array();
foreach ( $qv['fields'] as $field )
$this->query_fields[] = $wpdb->users . '.' . esc_sql( $field );
$this->query_fields = implode( ',', $this->query_fields );
} else {
$this->query_fields = "$wpdb->users.ID";
}
$this->query_from = "FROM $wpdb->users";
$this->query_where = "WHERE 1=1";
// sorting // sorting
if ( in_array( $qv['orderby'], array('nicename', 'email', 'url', 'registered') ) ) { if ( in_array( $qv['orderby'], array('nicename', 'email', 'url', 'registered') ) ) {
@ -437,14 +449,14 @@ class WP_User_Query {
$order = 'ASC'; $order = 'ASC';
else else
$order = 'DESC'; $order = 'DESC';
$this->query_orderby = " ORDER BY $orderby $order"; $this->query_orderby = "ORDER BY $orderby $order";
// limit // limit
if ( $qv['number'] ) { if ( $qv['number'] ) {
if ( $qv['offset'] ) if ( $qv['offset'] )
$this->query_limit = $wpdb->prepare(" LIMIT %d, %d", $qv['offset'], $qv['number']); $this->query_limit = $wpdb->prepare("LIMIT %d, %d", $qv['offset'], $qv['number']);
else else
$this->query_limit = $wpdb->prepare(" LIMIT %d", $qv['number']); $this->query_limit = $wpdb->prepare("LIMIT %d", $qv['number']);
} }
$search = trim( $qv['search'] ); $search = trim( $qv['search'] );
@ -510,13 +522,17 @@ class WP_User_Query {
function query() { function query() {
global $wpdb; global $wpdb;
$this->results = $wpdb->get_col("SELECT $wpdb->users.ID" . $this->query_from . $this->query_where . $this->query_orderby . $this->query_limit); if ( is_array( $this->query_vars['fields'] ) ) {
$this->results = $wpdb->get_results("SELECT $this->query_fields $this->query_from $this->query_where $this->query_orderby $this->query_limit");
} else {
$this->results = $wpdb->get_col("SELECT $this->query_fields $this->query_from $this->query_where $this->query_orderby $this->query_limit");
}
if ( !$this->results ) if ( !$this->results )
return; return;
if ( $this->query_vars['count_total'] ) if ( $this->query_vars['count_total'] )
$this->total_users = $wpdb->get_var("SELECT COUNT($wpdb->users.ID)" . $this->query_from . $this->query_where); $this->total_users = $wpdb->get_var("SELECT COUNT($wpdb->users.ID) $this->query_from $this->query_where $this->query_orderby $this->query_limit");
if ( 'all' == $this->query_vars['fields'] ) { if ( 'all' == $this->query_vars['fields'] ) {
cache_users($this->results); cache_users($this->results);
@ -967,7 +983,10 @@ function wp_dropdown_users( $args = '' ) {
$r = wp_parse_args( $args, $defaults ); $r = wp_parse_args( $args, $defaults );
extract( $r, EXTR_SKIP ); extract( $r, EXTR_SKIP );
$users = get_users( wp_array_slice_assoc( $r, array( 'blog_id', 'include', 'exclude', 'orderby', 'order' ) ) ); $query_args = wp_array_slice_assoc( $r, array( 'blog_id', 'include', 'exclude', 'orderby', 'order' ) );
$query_args['fields'] = array( 'ID', $show );
$users = get_users( $query_args );
$output = ''; $output = '';
if ( !empty($users) ) { if ( !empty($users) ) {