Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I know there are very similar questions ans answers out there already, but I simply cannot get my query to get into the right order.

I have a custom post type ("bb_articles") with custom fields, among them: - "bb_source": radio button with stored values 1, 2, 3... - "bb_date": date field

I want to show posts of specific "bb_source" (e.g. 1) ordered by "bb_date".

What I get, however, is a order by publication date (it seems), what is NOT what I want.

My code is:

$query = array (
   'post_type' => 'bb_articles',
   'meta-key' => 'bb_date',
   'orderby' => 'meta_value_num',
   'order' => 'DESC',
   'meta_query' => array(
      array('key' => 'bb_source',
        'value' => 1,
        'compare' => '='
      )
   )
);

$my_query = new WP_Query($query);

I also tried it that way as described by Hameedullah Khan here

$query = array (
   'post_type' => 'bb_articles',
   'meta-key' => 'bb_date',
   'orderby' => 'meta_value_num',
   'order' => 'DESC',
   'meta_query' => array(
         array (
            'key' => 'bb_date'
            ),
         array('key' => 'bb_source',
            'value' => 1,
            'compare' => '=='
            )
         )
);

"bb_date" is stored in Unix date format, i.e. 4 Nov 2012 is: 1351987200 thus, ordering by meta_value_num should imo produce the right order.

Obviously it does not, so where could the error be?

Any help is greatly appreciated. Thanks

Edit: cleaned code snipped and added second code snippet

share|improve this question

1 Answer 1

up vote 0 down vote accepted

It is meta_key not meta-key meaning you aren't meeting all of the conditions needed for this to work.

'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'.

share|improve this answer
    
Oh, for crying out loud! That did cost me HOURS now, and I just did not see it. Very embarrassing, indeed! Thanks for your help. –  blu bla Nov 4 '12 at 19:58

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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