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

In this article

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

Parameters

$searchstringrequired
Search string.
$columnsstring[]required
Array of columns to search.
$wildbooloptional
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:false

Return

string

Source

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

Changelog

VersionDescription
3.1.0Introduced.

User Contributed Notes

You must log in before being able to contribute a note or feedback.