I'm trying to order products by multiple meta keys, so that the most selled products appear first and the out of stock products lastly.
I managed to make the out of stock products show in the end, but the most selled are not ordering correctly.
I'm testing in this cenario, 5 products, 2 out of stock (showing in the end), the other 3 are being ordered like this, less selled first, most selled second and the last...
$meta_query = array('relation' => 'AND');
$meta_query[] = array(
'key' => '_stock_status',
);
$meta_query[] = array(
'key' => 'total_sales',
);
$orderby = array(
'_stock_status' => 'ASC',
'total_sales' => 'DESC',
);
$query->set( 'meta_query', $meta_query );
$query->set( 'orderby', $orderby );
_stock_status
could very easily be a custom taxonomy, which would be thousands of times faster to query – Tom J Nowell♦ May 10 '18 at 23:00