WC_Report_Customer_List::order_by_last_name()publicWC 1.0

Order users by name.

Method of the class: WC_Report_Customer_List{}

No Hooks.

Return

WP_User_Query.

Usage

$WC_Report_Customer_List = new WC_Report_Customer_List();
$WC_Report_Customer_List->order_by_last_name( $query );
$query(WP_User_Query) (required)
Query that gets passed through.

WC_Report_Customer_List::order_by_last_name() code WC 9.3.3

public function order_by_last_name( $query ) {
	global $wpdb;

	$s = ! empty( $_REQUEST['s'] ) ? wp_unslash( $_REQUEST['s'] ) : ''; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized

	$query->query_from   .= " LEFT JOIN {$wpdb->usermeta} as meta2 ON ({$wpdb->users}.ID = meta2.user_id) ";
	$query->query_where  .= " AND meta2.meta_key = 'last_name' ";
	$query->query_orderby = ' ORDER BY meta2.meta_value, user_login ASC ';

	if ( $s ) {
		$query->query_from   .= " LEFT JOIN {$wpdb->usermeta} as meta3 ON ({$wpdb->users}.ID = meta3.user_id)";
		$query->query_where  .= " AND ( user_login LIKE '%" . esc_sql( str_replace( '*', '', $s ) ) . "%' OR user_nicename LIKE '%" . esc_sql( str_replace( '*', '', $s ) ) . "%' OR meta3.meta_value LIKE '%" . esc_sql( str_replace( '*', '', $s ) ) . "%' ) ";
		$query->query_orderby = ' GROUP BY ID ' . $query->query_orderby;
	}

	return $query;
}