wc_products_array_orderby()WC 3.0.0

Sort an array of products by a value.

No Hooks.

Return

Array.

Usage

wc_products_array_orderby( $products, $orderby, $order );
$products(array) (required)
List of products to be ordered.
$orderby(string)
Optional order criteria.
Default: 'date'
$order(string)
Ascending or descending order.
Default: 'desc'

Changelog

Since 3.0.0 Introduced.

wc_products_array_orderby() code WC 9.3.3

function wc_products_array_orderby( $products, $orderby = 'date', $order = 'desc' ) {
	$orderby = strtolower( $orderby );
	$order   = strtolower( $order );
	switch ( $orderby ) {
		case 'title':
		case 'id':
		case 'date':
		case 'modified':
		case 'menu_order':
		case 'price':
			usort( $products, 'wc_products_array_orderby_' . $orderby );
			break;
		case 'none':
			break;
		default:
			shuffle( $products );
			break;
	}
	if ( 'desc' === $order ) {
		$products = array_reverse( $products );
	}
	return $products;
}