2

I have created a custom post type along with custom columns. One of my columns displays a date range such as "12/05/2012 - 11:00 am to 12/15/2012 - 1:00 pm". This is a concatenated string using a combination of four fields: startDate, startTime, endDate, endTime.

So far I have been able to make this column sortable based upon the startDate meta_key, but I would like to extend this such that it's sorted by the startDate and then by the startTime.

Currently

  • 12/23/2012 - 7:00 pm
  • 12/05/2012 - 11:00 am
  • 12/05/2012 - 2:00 pm
  • 11/30/2012 - 1:00 pm

Need

  • 12/23/2012 - 7:00 pm
  • 12/05/2012 - 2:00 pm
  • 12/05/2012 - 11:00 am
  • 11/30/2012 - 1:00 pm

This is my progress from what I've gathered over the internet so far, but it does not sort by time:

add_filter( 'request', 'event_column_orderby' );

function event_column_orderby ( $vars ) {

    if ( !isset( $vars['orderby'] ) || isset( $vars['orderby'] ) && 'datetime' == $vars['orderby'] ) {
        $vars = array_merge( $vars, array(
            'orderby'       => 'meta_value',
            'order'         => 'ASC',
            'meta_query'    => array(
                array(
                    'key'   => 'rqp-event-startDate'
                ),
                array(
                    'key'   => 'rqp-event-startTime'
                )
            )
        ));
    }

    return $vars;
}

1 Answer 1

1

Store the date time as a single key in the format yyyy-mm-dd hh:mm:ss.

2
  • Is the format what's causing it to not work? I tried something similar this morning by combining the $startDate and $startTime into $sortDate by using strtotime("{$startDate} {$startTime}"); Unfortunately this didn't work. Dec 30, 2012 at 18:01
  • Your date format wont work as is, though it may not be apparent until you add events in different years. The format should be in descending units from large to small. Adding the time in there just simplifies sorting, they'll naturally order themselves. Think of it from a purely numerical perspective- MySQL isnt aware of your date format, they're just numbers that it orders from large to small or vice versa
    – Milo
    Dec 30, 2012 at 18:12

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.