admin管理员组

文章数量:1425869

I have the following WP_Query:

$query = new WP_Query(array(
  'post_type'       => 'some_cpt',
  'posts_per_page'  => -1,
  'meta_key'        => 'active',
  'meta_value'      => 1,
  'orderby'         => 'ranking',
  'order'           => 'ASC'
));

Both active and raking are ACF fields, True/False and Numeric respectively. I am trying to get all some_cpt posts that have active set to true and at the same time order them by ranking. However, the code above totally ignores orderby.

I have the following WP_Query:

$query = new WP_Query(array(
  'post_type'       => 'some_cpt',
  'posts_per_page'  => -1,
  'meta_key'        => 'active',
  'meta_value'      => 1,
  'orderby'         => 'ranking',
  'order'           => 'ASC'
));

Both active and raking are ACF fields, True/False and Numeric respectively. I am trying to get all some_cpt posts that have active set to true and at the same time order them by ranking. However, the code above totally ignores orderby.

Share Improve this question asked May 27, 2019 at 18:03 DimChtzDimChtz 1778 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

You have to use "orderby" => "meta_value_num" and set a "meta_query" at the same time. Then choose the "meta_key" you want to order by.

Try:

$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',
    )
);

本文标签: advanced custom fieldsWP Query with meta queries