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 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;
}
share|improve this question
add comment

1 Answer

up vote 0 down vote accepted

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

share|improve this answer
    
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. –  strykern9ne Dec 30 '12 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 '12 at 18:12
add comment

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.