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');
}
}