您必须使用post_clauses
筛选以创建一些自定义SQL:
add_filter( \'posts_clauses\', \'wpse155827_price_sort\', 10, 2 );
function wpse155827_price_sort( $clauses, $wp_query ) {
$orderby = $wp_query->get( \'orderby\' );
$order = ( $wp_query->get( \'order\' ) == \'desc\') ? \'DESC\' : \'ASC\';
if( \'price\' === $orderby ) {
$clauses[\'join\'] .= " LEFT JOIN {$wpdb->postmeta} price ON( {$wpdb->posts }.ID = price.post_id AND price.meta_key = \'price\') ";
$clauses[\'orderby\'] = " CONVERT( REPLACE(price.meta_value, \'$\', \'\'), DECIMAL(13,2) ) " . $order;
}
return $clauses;
}
基本上,这是一个过滤器,每当您设置
orderby
到
price
. 在mySQL代码中,它接受元值,删除
$
符号,则将其转换为十进制。这将允许它现在正确排序。