Orderby两个元字段不起作用

时间:2014-10-03 作者:jcottone

祝你好运。我有以下代码:

$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

希望这有意义。。。请帮忙!

1 个回复
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\');

结束

相关推荐

管理分类术语-Orderby术语字段

我有一个自定义值,该值存储为term_group 但是在管理面板中,我想按术语组排序。我可以通过这个函数来排序,但有一个问题:function change_term_order( $args, $taxonomies ) { if ( is_admin() && \'tax_products\' !== $taxonomies[0] ) return $args; $args[\'orderby\'] = \'term_g