如何对多个wordpress自定义字段值进行排序? [英] How to sort multiple wordpress custom field values?
本文介绍了如何对多个wordpress自定义字段值进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
显示按价格"自定义字段排序的产品"类型的帖子:
Display posts with 'Product' type ordered by 'Price' custom field:
$query = new WP_Query(
array ( 'post_type' => 'product',
'orderby' => 'meta_value',
'meta_key' => 'price' )
);
如果还想按尺寸"订购,应该使用哪个代码?
Which code should I use if also want to order by 'Size'?
另一个示例,在该示例上,我需要对自定义字段进行多种排序:
Another example on which I need multiple sort on custom fields:
显示事件"类型的帖子,这些帖子按开始时间"和开始时间"排序.
Display posts with 'Event' type ordered by 'Start_Hour' and then by 'Start_Minute'.
推荐答案
感谢 Banetnet 我找到了解决方案:
Thanks to Bainternet I found the solution:
function orderbyreplace($orderby) {
return str_replace('menu_order', 'mt1.meta_value, mt2.meta_value', $orderby);
}
和...
$args = array(
'post_type'=>'Events',
'orderby' => 'menu_order',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'Start_Hour',
'value' => '',
'compare' => 'LIKE'
),
array(
'key' => 'Start_Minute',
'value' => '',
'compare' => 'LIKE'
)
)
);
add_filter('posts_orderby','orderbyreplace');
$loop = new WP_Query( $args );
remove_filter('posts_orderby','orderbyreplace');
这篇关于如何对多个wordpress自定义字段值进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文