How do I set the default admin sort order for a custom post type to a custom column?

SOLUTION AVAILABLE HERE

I’ve set up a custom post type called clientarea, and set up several custom columns for it in the admin area – the custom columns are all custom meta fields, as you can see from my code. I’d like to sort by ‘Appointment Date’ descending by default.

All of the columns work fine, and can be sorted manually as expected, but I can’t get the default sort order to work.

If I change the default sort field to a standard field (e.g. ‘title’) it works as expected; it only seems not to work when I’m trying to set a custom column as the default sort order. The order works (i.e. I can change between asc and desc by default even with a custom column), but it’s not picking up the orderby so is reverting back to sorting by the date the custom post was published.

What am I missing?

My code is as follows:

add_action( 'manage_posts_custom_column' , 'custom_columns', 10, 2 );

function custom_columns( $column, $post_id ) {
    global $wpdb;
    switch ( $column ) {
        case 'extranet_case_office':
            $get_office_ID = get_post_meta( $post_id, 'extranet_case_office', true );
            $get_office_name = $wpdb->get_results('SELECT post_title FROM `cn_bf_posts` WHERE `ID` = '.$get_office_ID);
            echo $get_office_name[0]->post_title;
            break;
        case 'extranet_appointment_date':
            echo date('d/m/Y',strtotime(get_post_meta( $post_id, 'extranet_appointment_date', true ))); 
            break;
        case 'extranet_appointment_type':
            echo get_post_meta( $post_id, 'extranet_appointment_type', true ); 
            break;
        case 'extranet_insolvency_practioner':
            $get_person_ID = get_post_meta( $post_id, 'extranet_insolvency_practioner', true );
            $get_person_name = $wpdb->get_results('SELECT post_title FROM `cn_bf_posts` WHERE `ID` = '.$get_person_ID);
            echo $get_person_name[0]->post_title;
            break;
        default:
            break;
    }
}

add_filter( 'manage_edit-clientarea_sortable_columns', 'my_sortable_clientarea_columns' );

function my_sortable_clientarea_columns( $columns ) {
    $columns['extranet_case_office'] = 'extranet_sort_office';
    $columns['extranet_appointment_date'] = 'extranet_sort_date';
    $columns['extranet_appointment_type'] = 'extranet_sort_type';
    $columns['extranet_insolvency_practioner'] = 'extranet_sort_IP';
    return $columns;
}

add_action( 'pre_get_posts', 'extranet_orderby' );

function extranet_orderby( $query ) {
    if( ! is_admin() )
        return;

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

    switch ( $orderby ) {
        case 'extranet_sort_office':
            $query->set('meta_key','extranet_case_office');
            $query->set('orderby','meta_value_num');
            break;
        case 'extranet_sort_date':
            $query->set('meta_key','extranet_appointment_date');
            $query->set('orderby','meta_value');
            break;
        case 'extranet_sort_type':
            $query->set('meta_key','extranet_appointment_type');
            $query->set('orderby','meta_value');
            break;
        case 'extranet_sort_IP':
            $query->set('meta_key','extranet_insolvency_practioner');
            $query->set('orderby','meta_value_num');
            break;
        default:
            break;
    }
}

add_action('pre_get_posts','clientarea_default_order');
function clientarea_default_order( $query ){
    if( $query->get('post_type')=='clientarea' ){
        if( $query->get('orderby') == '' )
            $query->set('orderby','extranet_sort_date');

        if( $query->get('order') == '' )
            $query->set('order','desc');
    }
}

1

Solution from a cross post over at StackExchange from @birgire:

The problem is that you run the clientarea_default_order callback
too late.

To fix that you only have to change the priority from the default one
that’s 10:

add_action( 'pre_get_posts','clientarea_default_order');

to the priority of 9:

add_action( 'pre_get_posts','clientarea_default_order', 9 );

But you don’t actually need two pre_get_posts callbacks.

You can combine them:

Example #1

is_admin() && add_action( 'pre_get_posts', 'extranet_orderby' );    

function extranet_orderby( $query ) 
{   
    // Nothing to do:  
    if( ! $query->is_main_query() || 'clientarea' != $query->get( 'post_type' )  )
        return;

    //-------------------------------------------  
    // Modify the 'orderby' and 'meta_key' parts
    //-------------------------------------------  
    $orderby = $query->get( 'orderby');      

    switch ( $orderby ) 
    {
        case 'extranet_sort_office':
            $query->set( 'meta_key', 'extranet_case_office' );
            $query->set( 'orderby',  'meta_value_num' );
            break;
        case 'extranet_sort_date':
            $query->set( 'meta_key', 'extranet_appointment_date' );  
            $query->set( 'orderby',  'meta_value' );
            break;
        case '':  // <-- The default empty case
            $query->set( 'meta_key', 'extranet_appointment_date' );  
            $query->set( 'orderby',  'meta_value' );
            break;
        case 'extranet_sort_type':
            $query->set( 'meta_key', 'extranet_appointment_type' );
            $query->set( 'orderby',  'meta_value' );
            break;
        case 'extranet_sort_IP':
            $query->set( 'meta_key', 'extranet_insolvency_practioner' );
            $query->set( 'orderby', 'meta_value_num' );
            break;
        default:
            break;
    }
}

where we added a main query check and an empty switch case.

Example #2

Here’s another approach, without the switch part (PHP 5.4+):

is_admin() && add_action( 'pre_get_posts', 'extranet_orderby' );

function extranet_orderby( $query )
{
    // Nothing to do
    if( ! $query->is_main_query() || 'clientarea' != $query->get( 'post_type' )  )
        return;

    //-------------------------------------------  
    // Modify the 'orderby' and 'meta_key' parts
    //-------------------------------------------  
    $orderby = strtolower( $query->get( 'orderby') );  
    $mods = [
        'office' => [ 'meta_key' => 'extranet_sort_office',           'orderby' => 'meta_value_num' ],
        'date'   => [ 'meta_key' => 'extranet_appointment_date',      'orderby' => 'meta_value'     ],
        ''       => [ 'meta_key' => 'extranet_appointment_date',      'orderby' => 'meta_value'     ],
        'type'   => [ 'meta_key' => 'extranet_sort_type',             'orderby' => 'meta_value_num' ],
        'ip'     => [ 'meta_key' => 'extranet_insolvency_practioner', 'orderby' => 'meta_value_num' ],
    ];
    $key = 'extranet_sort_' . $orderby;
    if( isset( $mods[$key] ) )
    {
        $query->set( 'meta_key', $mods[$key]['meta_key'] );
        $query->set( 'orderby',  $mods[$key]['orderby']  );
    }
}

Leave a Comment