16

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');
    }
}
8
  • Following - I've tried this in the past for a client with mixed results. Eager to see a good solution to this one.
    – jdm2112
    Commented Jul 15, 2015 at 14:44
  • Were you able to solve your sorting for custom columns?
    – jdm2112
    Commented Jul 21, 2015 at 20:44
  • @jdm2112 - yep - I cross posted this on Stack Exchange (because I had enough rep to set a bounty there..) - both of the given answers are correct, but the accepted one gives more explanation and some suggested code improvements. stackoverflow.com/questions/31434373/… Commented Jul 27, 2015 at 8:48
  • 1
    You should add a proper solution. You can copy the solution from your post on Stack Overflow, or a combination of all answers and then just post the link as form of credit to the original authors. I know @birgire would mind a repost with credit ;-) Commented Jul 27, 2015 at 8:53
  • 2
    Glad to hear it helped with the problem, just noticed the question here on WPSE. @PieterGoosen thanks for your suggestions, "to mind or not to mind" that's the epic question 😃
    – birgire
    Commented Jul 27, 2015 at 10:28

1 Answer 1

12

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']  );
    }
}
1
  • 1
    Great, thanks for posting a complete answer and with credit. +1 Commented Jul 27, 2015 at 9:21

Not the answer you're looking for? Browse other questions tagged or ask your own question.