Sort custom meta column by other meta value
I have a post type named "event" and I store two meta values for these posts: _start_time and _start_time_unix. _start_time contains string representation of a date and the other one a unix timestamp.
I only want to show the _start_time meta as a column on edit.php but it has to be sortable by the order of, you guessed it, _start_time_unix. This is because the string meta value can sometimes contain time and sometimes not, and even then the date format may end up being a kind that’s not any good for sorting.
Heres the code i use
add_filter('manage_event_posts_columns', 'zz_events_columns');
function zz_events_columns( $columns ) {
$columns['event_start'] = 'Starting';
...
return $columns;
}
add_filter( 'manage_edit-event_sortable_columns', 'zz_events_sortable_columns' );
function zz_events_sortable_columns( $columns ) {
$columns['event_start'] = 'Starting';
...
return $columns;
}
add_action( 'manage_event_posts_custom_column', 'zz_events_columns_render', 10, 2 );
function zz_events_columns_render( $column_name, $post_id ) {
if( $column_name == 'event_start' ) {
$event_start = get_post_meta( $post_id, '_event_start', true );
echo $event_start;
}
...
}
add_action( 'pre_get_posts', 'zz_events_columns_orderby' );
function zz_events_columns_orderby( $query ) {
global $pagenow;
if ( ! is_admin() || 'edit.php' != $pagenow || 'event' != $query->get( 'post_type' ) ) {
return;
}
$orderby = $query->get( 'orderby' );
switch ( $orderby ) {
case 'event_start':
$query->set( 'meta_key', '_event_start_unix' );
$query->set( 'orderby', 'meta_value_num' );
break;
...
default:
break;
}
}
I was thinking that it would work if I just set ‘meta_key’ to ‘_event_start_unix’ for the ‘event_start’ case but it still sorts wrong. Not sure if what I’m trying to achieve is even possible..
Leave an answer
You must login or register to add a new answer .