Order WooCommerce "Orders" by multiple custom meta fields

33 Views Asked by At

I am trying to set a custom sort on the WooCommerce Orders page based on multiple meta keys, and am not having any luck. To be clear, custom__num_skus_total and _order_total are numeric, but custom__sku_most_freq is a string.

This is the code I have so far, which the Orders page seems to disregard. None of my sorting is propagating

add_action( 'parse_query', 'custom_set_sorting' );
function custom_set_sorting( $query ) {
  $meta_query = array(
    'relation' => 'AND',
    'num_skus_total' => array(
      'key' => 'custom__num_skus_total',
      'type' => 'numeric',
      'compare' => 'EXISTS'
    ),
    'sku_most_freq' => array(
      'key' => 'custom__sku_most_freq',
      'compare' => 'EXISTS'
    ),
    'order_total' => array(
      'key' => '_order_total',
      'type' => 'numeric',
      'compare' => 'EXISTS'
    )
  );
  $orderby = array(
    'num_skus_total' => 'ASC',
    'sku_most_freq' => 'ASC',
    'order_total' => 'ASC'
  );
  $query->set( 'meta_query', $meta_query );
  $query->set( 'orderby', $orderby );
}
0

There are 0 best solutions below