wc_products_array_orderby

Sort an array of products by a value.

Description

(array) wc_products_array_orderby( (array) $products, (string) $orderby = 'date', (string) $order = 'desc' ); 

Returns (array)

Parameters (3)

0. $products (array)
The products.
1. $orderby — Optional. (string) => 'date'
The orderby.
2. $order — Optional. (string) => 'desc'
The order.

Usage

  1. if ( !function_exists( 'wc_products_array_orderby' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'woocommerce/includes/wc-product-functions.php'; 
  3.  
  4. // The products. 
  5. $products = array(); 
  6.  
  7. // The orderby. 
  8. $orderby = 'date'; 
  9.  
  10. // The order. 
  11. $order = 'desc'; 
  12.  
  13. // NOTICE! Understand what this does before running. 
  14. $result = wc_products_array_orderby($products, $orderby, $order); 
  15.  

Defined (1)

The function is defined in the following location(s).

/includes/wc-product-functions.php  
  1. function wc_products_array_orderby( $products, $orderby = 'date', $order = 'desc' ) { 
  2. $orderby = strtolower( $orderby ); 
  3. $order = strtolower( $order ); 
  4. switch ( $orderby ) { 
  5. case 'title' : 
  6. case 'id' : 
  7. case 'date' : 
  8. case 'modified' : 
  9. case 'menu_order' : 
  10. case 'price' : 
  11. usort( $products, 'wc_products_array_orderby_' . $orderby ); 
  12. break; 
  13. default : 
  14. shuffle( $products ); 
  15. break; 
  16. if ( 'desc' === $order ) { 
  17. $products = array_reverse( $products ); 
  18. return $products;