WP_User_Query::get_search_sql() WordPress Method

The WP_User_Query::get_search_sql() method is used to get the SQL code for searching users. It takes two parameters: the search string and the array of columns to search. The method returns an SQL string.

WP_User_Query::get_search_sql( string $search, string[] $columns, bool $wild = false ) #

Used internally to generate an SQL string for searching across multiple columns.


Parameters

$search

(string)(Required)Search string.

$columns

(string[])(Required)Array of columns to search.

$wild

(bool)(Optional)Whether to allow wildcard searches. Default is false for Network Admin, true for single site. Single site allows leading and trailing wildcards, Network Admin only trailing.

Default value: false


Top ↑

Return

(string)


Top ↑

Source

File: wp-includes/class-wp-user-query.php

	protected function get_search_sql( $search, $columns, $wild = false ) {
		global $wpdb;

		$searches      = array();
		$leading_wild  = ( 'leading' === $wild || 'both' === $wild ) ? '%' : '';
		$trailing_wild = ( 'trailing' === $wild || 'both' === $wild ) ? '%' : '';
		$like          = $leading_wild . $wpdb->esc_like( $search ) . $trailing_wild;

		foreach ( $columns as $column ) {
			if ( 'ID' === $column ) {
				$searches[] = $wpdb->prepare( "$column = %s", $search );
			} else {
				$searches[] = $wpdb->prepare( "$column LIKE %s", $like );
			}
		}

		return ' AND (' . implode( ' OR ', $searches ) . ')';
	}


Top ↑

Changelog

Changelog
VersionDescription
3.1.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.