0

within one of my query_posts I use 'meta_key' => 'clpr_topcoupon', ;meta_value'=> 1, now I would like to show post without as well but the one with meta_value first. I tried:

'meta_query' => array(
   'relation' => 'OR',
    array( //check to see if date has been filled out
            'meta_key' => 'clpr_topcoupon',
                        'meta_value'=> 1,
        ),
      array( //if no date has been added show these posts too
            'meta_key' => 'clpr_topcoupon',
                        'meta_value'=> 0,
        )
    ),

However it is not working. It shows the one with 0 first.

1 Answer 1

1
'meta_key' => 'clpr_topcoupon',
        'orderby' => 'meta_value_num',
        'order' => 'DESC'

You can solve the problem by using order by clause then there will no need to write meta query. Please use the above code to solve your problem.It will show the posts according to descending order of the value in meta key.

2
  • Just found one problem. I then need to order by publish date as well.
    – joloshop
    Feb 3, 2019 at 20:54
  • solved with 'meta_key' => 'clpr_topcoupon', 'orderby' => array( 'meta_value_num' => 'DESC', 'post_date' => 'DESC', ),
    – joloshop
    Feb 3, 2019 at 21:06

Your Answer

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

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