0

I have an acf field called totalviews. It stores the total views number for each page i have. The field is number/numeric. Then I am trying to find the page with most views so i sort the number i store in acf field using a wp_query inside functions.php file but i get wrong sort order results. Its messed up not ASC nor DESC ... is my query right? Any help appreciated.

$q_args = array(

                'category__not_in' => array( 62,63,170,175,176,171,180 ),
                'post_type' => 'post',
                'posts_per_page'=>'-1',
    'order'   => 'DESC',
                'meta_query' =>array(
                        array(
                            'key' => 'picture_url',
                            'value' => 'none',
                            'compare' => '!='
                       ),
                                 array(
                                         'key' => 'totalviews',
                                         'value' => '0',
                                         'compare' => '>=',
                                         'type' => 'NUMERIC',
                                         'orderby' => 'meta_value_num'
                                )
    )
            );
            $query = new WP_Query( $q_args );
6
  • The orderby doesn't belong in a meta_query. Instead, it's the main args and should be placed after the meta_query.
    – Sally CJ
    Jul 11, 2019 at 1:30
  • where exactly???
    – stefanosn
    Jul 11, 2019 at 1:35
  • 'meta_query' => array( ... ), 'orderby' => 'meta_value_num'
    – Sally CJ
    Jul 11, 2019 at 1:50
  • nope does not work! let me remove the array( 'key' => 'picture_url', 'value' => 'none', 'compare' => '!=' ),
    – stefanosn
    Jul 11, 2019 at 1:52
  • hmmm it worked without the array( 'key' => 'picture_url', 'value' => 'none', 'compare' => '!=' ) but i need to include it in the query... is it possible?
    – stefanosn
    Jul 11, 2019 at 1:53

1 Answer 1

3

From your comment:

it worked without the array( 'key' => 'picture_url', 'value' => 'none', 'compare' => '!=' ) but I need to include it in the query... is it possible?

Yes, using named sub-meta queries like so:

$q_args = [
    // ... other args here ...
    'meta_query' => [
        'picture_url' => [ 'key' => 'picture_url', 'value' => 'none', 'compare' => '!=' ],
        'totalviews'  => [ 'key' => 'totalviews', 'value' => '0', 'compare' => '>=', 'type' => 'NUMERIC' ],
    ],
    'orderby' => 'totalviews', // set this to a key in the above meta queries
    'order'   => 'DESC',
];

The orderby parameter can also be an array:

'orderby' => [
    'totalviews'  => 'DESC', // sort by meta_query key
    'picture_url' => 'ASC',  // sort by meta_query key
    'date'        => 'DESC', // sort by the post date
]

And (once again), orderby is not a valid argument for an array in the meta_query array. See here for more details.

1
  • 1
    Thanks for your answer it works great! I appreciate your help.
    – stefanosn
    Jul 11, 2019 at 15:29

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.