祝你好运。我有以下代码:
$args = array(
\'post_type\' => \'property\',
\'tax_query\' => array(
array(
\'taxonomy\' => \'rc_repl_status\',
\'field\' => \'slug\',
\'terms\' => \'sold\'
)
),
\'posts_per_page\' => 100,
\'meta_key\' => \'_rc_repl_property_year_sold\',
\'orderby\' => \'meta_value _rc_repl_property_acres\',
\'order\' => \'DESC\'
);
$query = new WP_Query( $args );
首先,我通过分类法过滤我的帖子类型,以查找已售出的房产(牧场列表)。下一步,我想知道他们第一次销售DESC订单的年份。接下来,我希望另一个已排序英亩的元字段(即\\u rc\\u repl\\u property\\u acres字段)也按DESC顺序排序。
这一年天气很好,然后在英亩地上休息。
如下所示:
牧场ASOLD 2014英亩1300
Branch BSOLD 2014英亩1700
RANCH CSOLD 2013ACRES 800
RANCH DSOLD 2013ACRES 900
----应该是----
Branch BSOLD 2014英亩1700
牧场ASOLD 2014英亩1300
RANCH DSOLD 2013ACRES 900
RANCH CSOLD 2013ACRES 800
希望这有意义。。。请帮忙!
SO网友:jcottone
特别感谢本文:http://dotnordic.se/sorting-wordpress-posts-by-several-numeric-custom-fields/
$args = array(
\'post_type\' => \'property\',
\'tax_query\' => array(
array(
\'taxonomy\' => \'rc_repl_status\',
\'field\' => \'slug\',
\'terms\' => \'sold\'
)
),
\'posts_per_page\' => 100,
\'meta_key\' => \'_rc_repl_property_year_sold\',
\'meta_query\' => array(
array(
\'key\' => \'_rc_repl_property_year_sold\' // do this to order by two meta field VALUES
),
array(
\'key\' => \'_rc_repl_property_acres\', // do this to order by two meta field VALUES
),
),
);
// do this to order by two meta field VALUES - This takes generic values and
// can be used with any WP_Query meta_query
function customorderby($orderby) {
return \'mt1.meta_value DESC, mt2.meta_value+0 DESC\';
}
// Now apply your filter. Add it before your query and remove it
// afterwards to make sure it doesn’t affect subsequent queries.
add_filter(\'posts_orderby\',\'customorderby\');
$query = new WP_Query( $args );
remove_filter(\'posts_orderby\',\'customorderby\');