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
.
1 Answer
Reset to default 0You 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',
)
);