最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

advanced custom fields - WP Query with meta queries

programmeradmin0浏览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.

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',
    )
);
发布评论

评论列表(0)

  1. 暂无评论