Order custom posts by a date metabox

When you’re saving the data on your custom post type you need to set it so that the meta box value is actually stored as a UNIX timestamp, eg:

if(!isset($_POST["event_informations_date"])) {
    return $post;
} else {
    $event_as_timestamp = strtotime ( $_POST["event_informations_date"];
    update_post_meta($post->ID, "event_informations_date", $event_as_timestamp );
}

This way you will have an orderable value for your meta keys. And you can query as per @helgatheviking

$args = array( 'post_type' => 'Event',
           'posts_per_page' => 1000,
           'order_by' => 'meta_value_num',
            'meta_value' => 'event_informations_date'  );

You will need to make sure that the add_meta_box() function you’re using converts the UNIX timestamp from saved custom field values back to the format you need.

Leave a Comment