WP_User_Search::prepare_query()publicWP 2.1.0

Prepares the user search query (legacy).

Method of the class: WP_User_Search{}

Hooks from the method

Return

null. Nothing (null).

Usage

$WP_User_Search = new WP_User_Search();
$WP_User_Search->prepare_query();

Notes

  • Global. wpdb. $wpdb WordPress database abstraction object.

Changelog

Since 2.1.0 Introduced.

WP_User_Search::prepare_query() code WP 6.6.2

public function prepare_query() {
	global $wpdb;
	$this->first_user = ($this->page - 1) * $this->users_per_page;

	$this->query_limit = $wpdb->prepare(" LIMIT %d, %d", $this->first_user, $this->users_per_page);
	$this->query_orderby = ' ORDER BY user_login';

	$search_sql = '';
	if ( $this->search_term ) {
		$searches = array();
		$search_sql = 'AND (';
		foreach ( array('user_login', 'user_nicename', 'user_email', 'user_url', 'display_name') as $col )
			$searches[] = $wpdb->prepare( $col . ' LIKE %s', '%' . like_escape($this->search_term) . '%' );
		$search_sql .= implode(' OR ', $searches);
		$search_sql .= ')';
	}

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

	if ( $this->role ) {
		$this->query_from .= " INNER JOIN $wpdb->usermeta ON $wpdb->users.ID = $wpdb->usermeta.user_id";
		$this->query_where .= $wpdb->prepare(" AND $wpdb->usermeta.meta_key = '{$wpdb->prefix}capabilities' AND $wpdb->usermeta.meta_value LIKE %s", '%' . $this->role . '%');
	} elseif ( is_multisite() ) {
		$level_key = $wpdb->prefix . 'capabilities'; // WPMU site admins don't have user_levels.
		$this->query_from .= ", $wpdb->usermeta";
		$this->query_where .= " AND $wpdb->users.ID = $wpdb->usermeta.user_id AND meta_key = '{$level_key}'";
	}

	do_action_ref_array( 'pre_user_search', array( &$this ) );
}