WP_User_Query::get_search_sql()protectedWP 3.1.0

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

Method of the class: WP_User_Query{}

No Hooks.

Return

String.

Usage

// protected - for code of main (parent) or child class
$result = $this->get_search_sql( $search, $columns, $wild );
$search(string) (required)
Search string.
$columns(string[]) (required)
Array of columns to search.
$wild(true|false)
Whether to allow wildcard searches. Single site allows leading and trailing wildcards, Network Admin only trailing.
Default: false for Network Admin, true for single site

Notes

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

Changelog

Since 3.1.0 Introduced.

WP_User_Query::get_search_sql() code WP 6.1.1

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 ) . ')';
}