Sort admin area by custom Event Date field

There are several questions in here about sorting custom items in the admin section. However, I cannot find a specific answer anywhere. Here’s my specific problem:

I’m using MagicFields to create several fields related to Events (each post is an “event” or concert). One of those fields is the event date. I want to 1) display the event date in the admin Event write panel as a separate column and 2) automatically sort that column ascending. So far, I’ve registered the column, displayed it, and have registered it as sortable. Here’s my code which is pasted in the functions.php file:

Read More
// Register a new column in the admin Post area
function event_date_column_register( $columns ) {
$columns['event_date'] = __( 'Event Date', 'my-plugin' );
return $columns;
}
add_filter('manage_edit-post_columns', 'event_date_column_register');

// Display the column content
function event_date_column_display( $column_name, $post_id ) {
if ( 'event_date' != $column_name )
    return;

$event_date = get('date_of_event');

echo $event_date;
}
add_action( 'manage_posts_custom_column', 'event_date_column_display', 10, 2 );


// Register the column as sortable
function event_date_column_register_sortable( $columns ) {
$columns['event_date'] = 'event_date';

return $columns;
}
add_filter( 'manage_edit-post_sortable_columns', 'event_date_column_register_sortable' );


function event_date_column_orderby( $vars ) {
if ( isset( $vars['orderby'] ) && 'event_date' == $vars['orderby'] ) {
    $vars = array_merge( $vars, array(
        'meta_key' => 'event_date',
        'orderby' => 'meta_value_num'
    ) );
}

return $vars;
}
add_filter( 'request', 'event_date_column_orderby' );

So, “Events” appear in the post management area as shown below – but when I try to sort by that field, all of my posts disappear.

Image Here

I know that I need to convert my date to the long unix time (strtotime();) prior to sorting, but where and how? According to the comment thread here (search for “works like a charm”), you can write some long query, but I don’t know if it’s correct, where to past that query, or anything else.

I’m a newbie – so thanks in advance for the help and sorry if I’m not being thorough enough.

Related posts

Leave a Reply

2 comments

  1. You need to change orderby value to meta_value in event_date_column_orderby, also I am not sure about the get('event_date') you are using, so I have replaced it in the code below to get_post_meta.

    I have just tested the code below and it sorts by dates just fine.

    // Register a new column in the admin Post area
    function event_date_column_register( $columns ) {
    $columns['event_date'] = __( 'Event Date', 'my-plugin' );
    return $columns;
    }
    add_filter('manage_edit-post_columns', 'event_date_column_register');
    
    // Display the column content
    function event_date_column_display( $column_name, $post_id ) {
    if ( 'event_date' != $column_name )
        return;
    
    $event_date = get_post_meta( $post_id, 'event_date', true );
    echo $event_date;
    }
    add_action( 'manage_posts_custom_column', 'event_date_column_display', 10, 2 );
    
    
    // Register the column as sortable
    function event_date_column_register_sortable( $columns ) {
    $columns['event_date'] = 'event_date';
    
    return $columns;
    }
    add_filter( 'manage_edit-post_sortable_columns', 'event_date_column_register_sortable' );
    
    
    function event_date_column_orderby( $vars ) {
    if ( isset( $vars['orderby'] ) && 'event_date' == $vars['orderby'] ) {
        $vars = array_merge( $vars, array(
            'meta_key' => 'event_date',
            'orderby' => 'meta_value'
        ) );
    }
    
    return $vars;
    }
    add_filter( 'request', 'event_date_column_orderby' );
    
  2. Is it possible to reverse the order of the output? I tried:

            $vars = array_merge( $vars, array(
            'meta_key' => 'tf_events_startdate',
            'orderby' => 'meta_value',
        'order' => 'DESC'
        ) 
    

    But no luck.