This one has been bugging me for a while. I've used Verve Meta Boxes plugin to add a date field to a custom post type I created, and I would like to somehow order by this field when using query_posts in PHP. What's the best way to do this? I haven't been able to figure out a way that didn't involve complicated custom queries.

  • one of my old days post.. check if it helps. Here i sorted by date saved in meta field wordpress.stackexchange.com/questions/6466/… – Sisir Sep 6 '11 at 18:16
  • Hello, I used this code: 'meta_key' => 'datum', 'orderby' => 'meta_value_num', 'order' => 'ASC' since I assumed the compare things were meant for only displaying events that had not yet past. This still does not work, for some reason. It's very frustrating. – CookieMonster Sep 6 '11 at 19:18
  • wordpress.stackexchange.com/questions/6466/… Hello, Can you post your full code here so i can take a look? What is the datum value is it the unix time? – Sisir Sep 7 '11 at 7:44
  • What is the format of your date field? – Milo Sep 7 '11 at 14:51

I found the following solution:

SELECT wposts . *
            FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta
            WHERE wposts.ID = wpostmeta.post_id
            AND wpostmeta.meta_key =  'datum'
            AND wposts.post_type =  'optredens'
            AND wposts.post_status =  'publish'
            ORDER BY wpostmeta.meta_value ASC 

As other query_posts() methods / alternatives did not work very well.

  • 1
    Ideally you should always use WordPress functions whenever possible rather than directly querying via MySQL, as it protects you against future database structure changes & can utilise WordPress' in-built caching for speed/performance. What you are trying to achieve is perfectly possible with get_posts, query_posts or by filtering the main query. – Simon Blackbourn Nov 8 '12 at 11:48

It would help to see all of your code, without more context it's just shots in the dark. I use this to accomplish what you're trying to do, using date format YYYY-MM-DD and it absolutely ‘works for me’.

global $wp_query;
$args = array(
    'meta_key' => 'datum',
    'orderby' => 'meta_value_num',
    'order' => 'ASC'
$args = array_merge( $wp_query->query, $args );
query_posts( $args );
  • I don't see why it's so much of a shot in the dark. I just have this meta field called datum (formatted properly YYYY-MM-DD through a custom date picker field), and other than that it's just a common wordpress query. I guess the issue might be related to some conflicting plug-in (custom order or such) that tries to change the ordering to it's own. – CookieMonster Sep 7 '11 at 16:43
  • @CookieMonster - it's a shot in the dark because you haven't posted the actual code you're using, just incomplete fragments. – Milo Sep 7 '11 at 16:56

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.