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

1 Answer 1

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
    )
),
1
  • Thank you very much, Shazzad, It works perfectly.
    – Max
    Commented Apr 12, 2020 at 21:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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