WP Query with meta queries

时间:2019-05-27 作者:DimChtz

我有以下WP\\U查询:

$query = new WP_Query(array(
  \'post_type\'       => \'some_cpt\',
  \'posts_per_page\'  => -1,
  \'meta_key\'        => \'active\',
  \'meta_value\'      => 1,
  \'orderby\'         => \'ranking\',
  \'order\'           => \'ASC\'
));
两者都有activeraking 是ACF字段,True/FalseNumeric 分别地我想得到所有some_cpt 具有active 设置为true,同时按排序ranking. 然而,上面的代码完全忽略了orderby.

1 个回复
最合适的回答,由SO网友:filipecsweb 整理而成

你必须使用"orderby" => "meta_value_num" 并设置"meta_query" 同时然后选择"meta_key" 您想按以下方式订购。

尝试:

$query = new WP_Query(
    array(
        \'post_type\'      => \'some_cpt\',
        \'posts_per_page\' => - 1,
        \'meta_query\'     => array(
            array(
                \'key\'     => \'active\',
                \'value\'   => \'1\',
                \'compare\' => \'=\',
            )
        ),
        \'meta_key\'       => \'ranking\',
        \'orderby\'        => \'meta_value_num\',
        \'order\'          => \'ASC\',
    )
);