WP_User_Query::prepare_query() – Prepares the query variables.

You appear to be a bot. Output may be restricted

Description

Prepares the query variables.

Usage

WP_User_Query::prepare_query( $query );

Parameters

$query
( string|array ) optional – { Optional. Array or string of Query parameters.
$blog_id
( int ) optional – The site ID. Default is the current site.
$role
( string|string[] ) optional – An array or a comma-separated list of role names that users must match to be included in results. Note that this is an inclusive list: users must match each role. Default empty.
$role__in
( string[] ) optional – An array of role names. Matched users must have at least one of these roles. Default empty array.
$role__not_in
( string[] ) optional – An array of role names to exclude. Users matching one or more of these roles will not be included in results. Default empty array.
$meta_key
( string|string[] ) optional – Meta key or keys to filter by.
$meta_value
( string|string[] ) optional – Meta value or values to filter by.
$meta_compare
( string ) optional – MySQL operator used for comparing the meta value. See WP_Meta_Query::__construct() for accepted values and default value.
$meta_compare_key
( string ) optional – MySQL operator used for comparing the meta key. See WP_Meta_Query::__construct() for accepted values and default value.
$meta_type
( string ) optional – MySQL data type that the meta_value column will be CAST to for comparisons. See WP_Meta_Query::__construct() for accepted values and default value.
$meta_type_key
( string ) optional – MySQL data type that the meta_key column will be CAST to for comparisons. See WP_Meta_Query::__construct() for accepted values and default value.
$meta_query
( array ) optional – An associative array of WP_Meta_Query arguments. See WP_Meta_Query::__construct() for accepted values.
$capability
( string|string[] ) optional – An array or a comma-separated list of capability names that users must match to be included in results. Note that this is an inclusive list: users must match each capability. Does NOT work for capabilities not in the database or filtered via map_meta_cap . Default empty.
$capability__in
( string[] ) optional – An array of capability names. Matched users must have at least one of these capabilities. Does NOT work for capabilities not in the database or filtered via map_meta_cap . Default empty array.
$capability__not_in
( string[] ) optional – An array of capability names to exclude. Users matching one or more of these capabilities will not be included in results. Does NOT work for capabilities not in the database or filtered via map_meta_cap . Default empty array.
$include
( int[] ) optional – An array of user IDs to include. Default empty array.
$exclude
( int[] ) optional – An array of user IDs to exclude. Default empty array.
$search
( string ) optional – Search keyword. Searches for possible string matches on columns. When $search_columns is left empty, it tries to determine which column to search in based on search string. Default empty.
$search_columns
( string[] ) optional – Array of column names to be searched. Accepts 'ID', 'user_login', 'user_email', 'user_url', 'user_nicename', 'display_name'. Default empty array.
$orderby
( string|array ) optional – Field(s) to sort the retrieved users by. May be a single value, an array of values, or a multi-dimensional array with fields as keys and orders ('ASC' or 'DESC') as values. Accepted values are:

  • 'ID'
  • 'display_name' (or 'name')
  • 'include'
  • 'user_login' (or 'login')
  • 'login__in'
  • 'user_nicename' (or 'nicename'),
  • 'nicename__in'
  • 'user_email (or 'email')
  • 'user_url' (or 'url'),
  • 'user_registered' (or 'registered')
  • 'post_count'
  • 'meta_value',
  • 'meta_value_num'
  • The value of $meta_key
  • An array key of $meta_query

To use 'meta_value' or 'meta_value_num', $meta_key must be also be defined. Default 'user_login'.

$order
( string ) optional – Designates ascending or descending order of users. Order values passed as part of an $orderby array take precedence over this parameter. Accepts 'ASC', 'DESC'. Default 'ASC'.
$offset
( int ) optional – Number of users to offset in retrieved results. Can be used in conjunction with pagination. Default 0.
$number
( int ) optional – Number of users to limit the query for. Can be used in conjunction with pagination. Value -1 (all) is supported, but should be used with caution on larger sites. Default -1 (all users).
$paged
( int ) optional – When used with number, defines the page of results to return. Default 1.
$count_total
( bool ) optional – Whether to count the total number of users found. If pagination is not needed, setting this to false can improve performance. Default true.
$fields
( string|string[] ) optional – Which fields to return. Single or all fields (string), or array of fields. Accepts:

  • 'ID'
  • 'display_name'
  • 'user_login'
  • 'user_nicename'
  • 'user_email'
  • 'user_url'
  • 'user_registered'
  • 'user_pass'
  • 'user_activation_key'
  • 'user_status'
  • 'spam' (only available on multisite installs)
  • 'deleted' (only available on multisite installs)
  • 'all' for all fields and loads user meta.
  • 'all_with_meta' Deprecated. Use 'all'.

Default 'all'.

$who
( string ) optional – Type of users to query. Accepts 'authors'. Default empty (all users).
$has_published_posts
( bool|string[] ) optional – Pass an array of post types to filter results to users who have published posts in those post types. true is an alias for all public post types.
$nicename
( string ) optional – The user nicename. Default empty.
$nicename__in
( string[] ) optional – An array of nicenames to include. Users matching one of these nicenames will be included in results. Default empty array.
$nicename__not_in
( string[] ) optional – An array of nicenames to exclude. Users matching one of these nicenames will not be included in results. Default empty array.
$login
( string ) optional – The user login. Default empty.
$login__in
( string[] ) optional – An array of logins to include. Users matching one of these logins will be included in results. Default empty array.
$login__not_in
( string[] ) optional – An array of logins to exclude. Users matching one of these logins will not be included in results. Default empty array. }

Returns

void

Source

File name: wordpress/wp-includes/class-wp-user-query.php
Lines:

1 to 100 of 509
  public function prepare_query( $query = array() ) {
    global $wpdb, $wp_roles;

    if ( empty( $this->query_vars ) || ! empty( $query ) ) {
      $this->query_limit = null;
      $this->query_vars  = $this->WP_User_Query::fill_query_vars( $query );
    }

    
/**
 * Fires before the WP_User_Query has been parsed.
 *
 * The passed WP_User_Query object contains the query variables,
 * not yet passed into SQL.
 *
 * @since 4.0.0
 *
 * @param WP_User_Query $query Current instance of WP_User_Query (passed by reference).
 */
    do_action_ref_array( 'pre_get_users', array( &$this ) );

    // Ensure that query vars are filled after 'pre_get_users'.
    $qv =& $this->query_vars;
    $qv = $this->WP_User_Query::fill_query_vars( $qv );

    $allowed_fields = array(
      'id',
      'user_login',
      'user_pass',
      'user_nicename',
      'user_email',
      'user_url',
      'user_registered',
      'user_activation_key',
      'user_status',
      'display_name',
    );
    if ( is_multisite() ) {
      $allowed_fields[] = 'spam';
      $allowed_fields[] = 'deleted';
    }

    if ( is_array( $qv['fields'] ) ) {
      $qv['fields'] = array_map( 'strtolower', $qv['fields'] );
      $qv['fields'] = array_intersect( array_unique( $qv['fields'] ), $allowed_fields );

      if ( empty( $qv['fields'] ) ) {
        $qv['fields'] = array( 'id' );
      }

      $this->query_fields = array();
      foreach ( $qv['fields'] as $field ) {
        $field                = 'id' === $field ? 'ID' : sanitize_key( $field );
        $this->query_fields[] = "$wpdb->users.$field";
      }
      $this->query_fields = implode( ',', $this->query_fields );
    } elseif ( 'all_with_meta' === $qv['fields'] || 'all' === $qv['fields'] || ! in_array( $qv['fields'], $allowed_fields, true ) ) {
      $this->query_fields = "$wpdb->users.ID";
    } else {
      $field              = 'id' === strtolower( $qv['fields'] ) ? 'ID' : sanitize_key( $qv['fields'] );
      $this->query_fields = "$wpdb->users.$field";
    }

    if ( isset( $qv['count_total'] ) && $qv['count_total'] ) {
      $this->query_fields = 'SQL_CALC_FOUND_ROWS ' . $this->query_fields;
    }

    $this->query_from  = "FROM $wpdb->users";
    $this->query_where = 'WHERE 1=1';

    // Parse and sanitize 'include', for use by 'orderby' as well as 'include' below.
    if ( ! empty( $qv['include'] ) ) {
      $include = wp_parse_id_list( $qv['include'] );
    } else {
      $include = false;
    }

    $blog_id = 0;
    if ( isset( $qv['blog_id'] ) ) {
      $blog_id = absint( $qv['blog_id'] );
    }

    if ( $qv['has_published_posts'] && $blog_id ) {
      if ( true === $qv['has_published_posts'] ) {
        $post_types = get_post_types( array( 'public' => true ) );
      } else {
        $post_types = (array) $qv['has_published_posts'];
      }

      foreach ( $post_types as &$post_type ) {
        $post_type = $wpdb->prepare( '%s', $post_type );
      }

      $posts_table        = $wpdb->get_blog_prefix( $blog_id ) . 'posts';
      $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 ( " . implode( ', ', $post_types ) . ' ) )';
    }

    // nicename
    if ( '' !== $qv['nicename'] ) {
      $this->query_where .= $wpdb->prepare( ' AND user_nicename = %s', $qv['nicename'] );
    }
 

 View on GitHub View on Trac