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

meta query - How to use orderby with meta_query?

programmeradmin4浏览0评论

I want to order posts by views after filtering them with meta_query by a specific meta_key.

Using this query I was able to order posts by views but I was unable to order only the posts with the 'meta_key' => 'my_choices'.

$args = array(
            'post_type'=> 'post',
            'posts_per_page'=> 24,
            'meta_key' => 'wpb_post_views_count',
            'orderby' => 'meta_value_num',
            'order' => 'DESC',
            'meta_query' => 
                array(
                    array(
                        'meta_key' => 'my_choices',
                        'meta_value' => '1',
                        )
                    ),
            );
$wp_query = new WP_Query($args);
$i=0; while ($wp_query->have_posts()) : $wp_query->the_post(); ++$i;

Anyone know how can this be done?

Thanks

I want to order posts by views after filtering them with meta_query by a specific meta_key.

Using this query I was able to order posts by views but I was unable to order only the posts with the 'meta_key' => 'my_choices'.

$args = array(
            'post_type'=> 'post',
            'posts_per_page'=> 24,
            'meta_key' => 'wpb_post_views_count',
            'orderby' => 'meta_value_num',
            'order' => 'DESC',
            'meta_query' => 
                array(
                    array(
                        'meta_key' => 'my_choices',
                        'meta_value' => '1',
                        )
                    ),
            );
$wp_query = new WP_Query($args);
$i=0; while ($wp_query->have_posts()) : $wp_query->the_post(); ++$i;

Anyone know how can this be done?

Thanks

Share Improve this question asked Apr 12, 2020 at 20:00 MaxMax 31 bronze badge
Add a comment  | 

1 Answer 1

Reset to default 2

You have used wrong parameters for meta_query.

It should be -

'meta_query' => array(
    array(
        'key' => 'my_choices', // not meta_key
        'value' => '1', // not meta_value
    )
),
发布评论

评论列表(0)

  1. 暂无评论