0

Could somebody tell me what's wrong with my WP query please? I'm trying to order my posts by price and price is an ACF (a textbox which has a number in it)

            $args = array (
                'post_type'     => 'product',
                'post__in'      => $post_ids,
                'posts_per_page' => -1,
                'order' => 'ASC',
                'orderby' => 'meta_value',
                'meta_key' => 'tour_price',
                'meta_query' => array(
                   array(
                   'key'       => 'tour_price',
                   'compare'   => '=',
                   'type'      => 'NUMERIC',
                   )
                ),
            );
0

2 Answers 2

1

'meta_value_num' - Order by numeric meta value (available with Version 2.8). Also note that a 'meta_key=keyname' must also be present in the query. This value allows for numerical sorting as noted above in 'meta_value'.

Try to use like this :

 $args = array (
            'post_type'     => 'product',
            'post__in'      => $post_ids,
            'posts_per_page' => -1,
            'order' => 'ASC',
            'orderby' => 'meta_value_num',
            'meta_key' => 'tour_price',
            'meta_query' => array(
               array(
               'key'       => 'tour_price',
               'compare'   => '=',
               'type'      => 'NUMERIC',
               )
            ),
        );
2
0

You don't need meta_query in your code. And also you don't need to check numeric value of meta field like that. To sort by numeric value, in your orderby parameter, you should use meta_value_num instead.

So this will be your WP_Query.

$args = array (
  'post_type'    => 'product',
  'post__in'      => $post_ids,
  'posts_per_page' => -1,
  'order' => 'ASC',
  'orderby' => 'meta_value_num',
  'meta_key' => 'tour_price',
);

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.