96
96
public static function fill_query_vars( $args ) {
98
'blog_id' => $GLOBALS['blog_id'],
98
'blog_id' => get_current_blog_id(),
100
100
'role__in' => array(),
101
101
'role__not_in' => array(),
115
115
'fields' => 'all',
117
117
'has_published_posts' => null,
119
'nicename__in' => array(),
120
'nicename__not_in' => array(),
122
'login__in' => array(),
123
'login__not_in' => array()
120
126
return wp_parse_args( $args, $defaults );
131
137
* @since 4.4.0 Added 'paged', 'role__in', and 'role__not_in' parameters. The 'role' parameter was updated to
132
138
* permit an array or comma-separated list of values. The 'number' parameter was updated to support
133
139
* querying for all users with using -1.
140
* @since 4.7.0 Added 'nicename', 'nicename__in', 'nicename__not_in', 'login', 'login__in',
141
* and 'login__not_in' parameters.
165
173
* an array of values, or a multi-dimensional array with fields as
166
174
* keys and orders ('ASC' or 'DESC') as values. Accepted values are
167
175
* 'ID', 'display_name' (or 'name'), 'include', 'user_login'
168
* (or 'login'), 'user_nicename' (or 'nicename'), 'user_email'
169
* (or 'email'), 'user_url' (or 'url'), 'user_registered'
170
* or 'registered'), 'post_count', 'meta_value', 'meta_value_num',
171
* the value of `$meta_key`, or an array key of `$meta_query`. To use
172
* 'meta_value' or 'meta_value_num', `$meta_key` must be also be
173
* defined. Default 'user_login'.
176
* (or 'login'), 'login__in', 'user_nicename' (or 'nicename'),
177
* 'nicename__in', 'user_email (or 'email'), 'user_url' (or 'url'),
178
* 'user_registered' (or 'registered'), 'post_count', 'meta_value',
179
* 'meta_value_num', the value of `$meta_key`, or an array key of
180
* `$meta_query`. To use 'meta_value' or 'meta_value_num', `$meta_key`
181
* must be also be defined. Default 'user_login'.
174
182
* @type string $order Designates ascending or descending order of users. Order values
175
183
* passed as part of an `$orderby` array take precedence over this
176
184
* parameter. Accepts 'ASC', 'DESC'. Default 'ASC'.
195
203
* @type bool|array $has_published_posts Pass an array of post types to filter results to users who have
196
204
* published posts in those post types. `true` is an alias for all
197
205
* public post types.
206
* @type string $nicename The user nicename. Default empty.
207
* @type array $nicename__in An array of nicenames to include. Users matching one of these
208
* nicenames will be included in results. Default empty array.
209
* @type array $nicename__not_in An array of nicenames to exclude. Users matching one of these
210
* nicenames will not be included in results. Default empty array.
211
* @type string $login The user login. Default empty.
212
* @type array $login__in An array of logins to include. Users matching one of these
213
* logins will be included in results. Default empty array.
214
* @type array $login__not_in An array of logins to exclude. Users matching one of these
215
* logins will not be included in results. Default empty array.
200
218
public function prepare_query( $query = array() ) {
270
288
$this->query_where .= " AND $wpdb->users.ID IN ( SELECT DISTINCT $posts_table.post_author FROM $posts_table WHERE $posts_table.post_status = 'publish' AND $posts_table.post_type IN ( " . join( ", ", $post_types ) . " ) )";
292
if ( '' !== $qv['nicename']) {
293
$this->query_where .= $wpdb->prepare( ' AND user_nicename = %s', $qv['nicename'] );
296
if ( ! empty( $qv['nicename__in'] ) ) {
297
$sanitized_nicename__in = array_map( 'esc_sql', $qv['nicename__in'] );
298
$nicename__in = implode( "','", $sanitized_nicename__in );
299
$this->query_where .= " AND user_nicename IN ( '$nicename__in' )";
302
if ( ! empty( $qv['nicename__not_in'] ) ) {
303
$sanitized_nicename__not_in = array_map( 'esc_sql', $qv['nicename__not_in'] );
304
$nicename__not_in = implode( "','", $sanitized_nicename__not_in );
305
$this->query_where .= " AND user_nicename NOT IN ( '$nicename__not_in' )";
309
if ( '' !== $qv['login']) {
310
$this->query_where .= $wpdb->prepare( ' AND user_login = %s', $qv['login'] );
313
if ( ! empty( $qv['login__in'] ) ) {
314
$sanitized_login__in = array_map( 'esc_sql', $qv['login__in'] );
315
$login__in = implode( "','", $sanitized_login__in );
316
$this->query_where .= " AND user_login IN ( '$login__in' )";
319
if ( ! empty( $qv['login__not_in'] ) ) {
320
$sanitized_login__not_in = array_map( 'esc_sql', $qv['login__not_in'] );
321
$login__not_in = implode( "','", $sanitized_login__not_in );
322
$this->query_where .= " AND user_login NOT IN ( '$login__not_in' )";
274
326
$this->meta_query = new WP_Meta_Query();
275
327
$this->meta_query->parse_query_vars( $qv );
431
$orderby_array[] = $parsed . ' ' . $this->parse_order( $_order );
483
if ( 'nicename__in' === $_orderby || 'login__in' === $_orderby ) {
484
$orderby_array[] = $parsed;
486
$orderby_array[] = $parsed . ' ' . $this->parse_order( $_order );
434
490
// If no valid clauses were found, order by user_login.
706
762
$include = wp_parse_id_list( $this->query_vars['include'] );
707
763
$include_sql = implode( ',', $include );
708
764
$_orderby = "FIELD( $wpdb->users.ID, $include_sql )";
765
} elseif ( 'nicename__in' === $orderby ) {
766
$sanitized_nicename__in = array_map( 'esc_sql', $this->query_vars['nicename__in'] );
767
$nicename__in = implode( "','", $sanitized_nicename__in );
768
$_orderby = "FIELD( user_nicename, '$nicename__in' )";
769
} elseif ( 'login__in' === $orderby ) {
770
$sanitized_login__in = array_map( 'esc_sql', $this->query_vars['login__in'] );
771
$login__in = implode( "','", $sanitized_login__in );
772
$_orderby = "FIELD( user_login, '$login__in' )";
709
773
} elseif ( isset( $meta_query_clauses[ $orderby ] ) ) {
710
774
$meta_clause = $meta_query_clauses[ $orderby ];
711
775
$_orderby = sprintf( "CAST(%s.meta_value AS %s)", esc_sql( $meta_clause['alias'] ), esc_sql( $meta_clause['cast'] ) );