Sorting WordPress Custom Meta Box value by date

I’m working on a custom events post type for a wordpress theme and I’m running into a sorting problem. Everything works as I need until I spit out the values to the template. I need the events to be listed in order by date, but I can’t seem to get that to work. Here’s my code:

// From my custom meta box. Shows the type of field that
// the user enters the date into. The prefix is _cmb_
array(
    'name' => 'Event Date',
    'id'   => $prefix . 'event_date',
    'type' => 'text_date'
),

---- // -----

// Grab the date that's input from the user in the
// Custom Meta Box and Convert the text_date to 
// YYYYMMDD format
if ( $field['type'] == 'text_date' ) {
    $new = date('omd', strtotime($new));
}

Above I’m getting the input from the date picker and reformatting the text.

Read More
// Tour Dates Loop
$args = array(
    'post_type' => 'event',
    'meta_key'  => '_cmb_event_date',
    'order_by'  => 'meta_value_num',
    'order'     => 'DESC'
);
$tourDates = new WP_Query( $args );

Here I’m using WP_Query to grab the proper post type, the meta key, and I’m trying to sort the output via that meta key. The only problem is that the events aren’t sorted properly.

I’ve tried converting the _cmb_event_date to be an integer, but I couldn’t get that to work properly. I thought that maybe the dates weren’t sorting right because they weren’t an integer, but a string.

I’ve looked through a bunch of posts here on Stack and I can’t seem to find an answer that works for me. Any help is much appreciated! If you need more context for the code, let me know.

Related posts

Leave a Reply

1 comment