WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have no idea why my sortable function does not sort my "events" my a custom post-meta. I have a custom-post-type named wr_event… I have my custom columns set up and my post-meta is shown in the columns, just like I want. The "Event-Date" handle even is already a sortable link! Only thing if I click on the sort-link for the event date the list doesn't update. The function is fired as I see the page reloading however it doesn't sort anyhing.

Even though the url toggles between those two states if I click the sort-link …



However the list always looks the same!

Here is my entire code for the custom columns:

add_filter("manage_edit-wr_event_columns", "wr_event_edit_columns");

function wr_event_edit_columns($columns) {
    $columns = array(
        "cb" => "<input type=\"checkbox\" />",
        "title" => "Event",
        "event_date" => "Event Date",
        "type" => "Type",

    return $columns;

add_action("manage_posts_custom_column",  "event_custom_columns");

function event_custom_columns($column) {
    global $post;

    switch ($column) {
        case "event_date":
        $custom = get_post_custom();
        echo $custom['_wr_event_date'][0];
        case "type":
        echo get_the_term_list($post->ID, 'event_type', '', ', ','');

add_filter("manage_edit-wr_event_sortable_columns", "wr_event_sortable_columns");   

function wr_event_sortable_columns( $columns ) {
    $columns['event_date'] = 'event_date';
    return $columns;

add_filter("request", "event_column_orderby");

function event_column_orderby( $vars ) {
    if ( isset( $vars['orderby'] ) && 'event_date' == $vars['event_date'] ) {
        $vars = array_merge( $vars,
                'meta_key'  => '_wr_event_date',
                'orderby'   => 'meta_value_num',
                'order'     => 'asc'

    return $vars;
share|improve this question
up vote 6 down vote accepted

It's because you are checking 'event_date' == $vars['event_date'] not 'event_date' == $vars['orderby].

But don't use the request filter. Instead:

add_action( 'pre_get_posts', 'event_column_orderby' );  
function event_column_orderby( $query ) {  
    if( ! is_admin() )  

    $orderby = $query->get( 'orderby');  

    if( 'event_date' == $orderby ) {  

... and don't manually set the order as you won't then see the difference when toggling between ASC/DESC.

share|improve this answer
Thank you, exactly this made me realise why it didn't work. I did already have an action added to pre_get_posts and didn't set a condition for is_admin() - thank you very much! – mathiregister Apr 17 '12 at 17:36
the is_admin() isn't essential here. I added it to stop it potentially interferometry on a non-admin page. :) – Stephen Harris Apr 17 '12 at 18:08
pre_get_posts action cracks the ball - Thanks – Gowri Mar 15 '13 at 6:19

Your Answer


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.