0

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.

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',
    )
);
  • Thank you. I am always confused with these meta queries :) – DimChtz May 27 at 18:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.