0

I used simple-fields plugin for one addition field(premium posts) and now I need to change the popular post widget. I need to get all popular post exclude premium post.

What I have tried so far,

original query,

$popularposts = new WP_Query('showposts=5&meta_key=post_views_count&orderby=meta_value_num&order=DESC&ignore_sticky_posts=1')

edited query,

$args1 = array(
    'showposts' => 5,
    'ignore_sticky_posts' => 1,
    'meta_query' => array(
        array( 'key' => 'post_views_count', 'orderby' => 'meta_value_num', 'order' => DESC ),
        array( 'key' => '_simple_fields_fieldGroupID_2_fieldID_1_numInSet_0', 'compare' =>'NOT EXISTS' ),
    ),
);
$popularposts = new WP_Query($args1);

But it doesn't provide expected results, it exclude premium posts(field : _simple_fields_fieldGroupID_2_fieldID_1_numInSet_0) but it doesn't sort by post_view_counts.

Any help would be appreciate..

1

The correct query is:

$args1 = array(
    'showposts'           => 5,
    'ignore_sticky_posts' => 1,
     //ORDER ARGUMENTS
    'meta_key'            => 'post_views_count',
    'orderby'             => 'meta_key_num',
    'order'               => 'DESC',
    //META QUERY
    'meta_query' => array(
        array(
            'key'     => '_simple_fields_fieldGroupID_2_fieldID_1_numInSet_0',
            'compare' =>'NOT EXISTS' ),
    ),
);
$popularposts = new WP_Query($args1);

The order aguments are always outside meta_query, see the codex.

1

You cannot sort inside a meta_query. The sorting parameters are invalid inside the meta_query.

You will need to move the sorting parameters to outside the meta_query and remember to set the meta_key which will be used for sorting

References:

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.