add_filter( 'posts_join','join_product_stock', 10, 2 );
function join_product_stock($join) {
global $wp_query, $wpdb;
$join .= " LEFT JOIN $wpdb->postmeta {$wpdb->prefix}stock_status ON {$wpdb->prefix}stock_status.post_id = {$wpdb->posts}.ID AND {$wpdb->prefix}stock_status.meta_key = '_stock_status' LEFT JOIN $wpdb->postmeta {$wpdb->prefix}stock ON {$wpdb->prefix}stock.post_id = {$wpdb->posts}.ID AND {$wpdb->prefix}stock.meta_key = '_stock'";
return $join;
}
add_filter('posts_orderby', 'product_stock_orderby');
function product_stock_orderby($orderby_statement) {
$orderby_statement = " wp_stock_status.meta_value ASC, wp_stock.meta_value ASC ";
return $orderby_statement;
}