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;
}
Store the date time as a single key in the format
yyyy-mm-dd hh:mm:ss
.