如果有人碰巧发现了这一点,我会给你一个解决方案,它使用“伪造的自然排序”(感谢Otto),但它也有多个自定义字段的排序。
function orderbyreplace($orderby ) {
global $wpdb;
$new = str_replace($wpdb->prefix.\'postmeta.meta_value ASC\', \'LENGTH(mt1.meta_value) ASC, mt1.meta_value ASC, mt2.meta_value ASC\', $orderby);
return $new;
}
function get_posts_by_tax_cat($taxonomy, $parent_cat_id) {
$args = array(
\'posts_per_page\' => -1,
\'post_type\' => \'product\',
\'orderby\' => \'meta_value\',
\'order\' => \'ASC\',
\'meta_key\' => \'flansoppning\',
\'meta_query\' => array(
array(
\'key\' => \'flansoppning\',
\'value\' => \'\',
\'compare\' => \'LIKE\'
),
array(
\'key\' => \'gangstorlek_mm\',
\'value\' => \'\',
\'compare\' => \'LIKE\'
)
),
\'no_found_rows\' => true,
\'cache_results\' => false,
\'include_children\' => false,
\'tax_query\' => array(
array(
\'taxonomy\' => $taxonomy,
\'field\' => \'id\',
\'terms\' => array($parent_cat_id)
)
)
);
//Filter makes it possible to sort on flansoppning (length() gives natural sort) first and then gangstorlek_mm
add_filter(\'posts_orderby\',\'orderbyreplace\');
$loop = new WP_Query( $args );
remove_filter(\'posts_orderby\',\'orderbyreplace\');
$posts = $loop->posts;
if(!empty($posts)) {
return $posts;
}
return array();
}