GIẢI PHÁP CÓ TẠI ĐÂY
Tôi đã thiết lập một loại bài đăng tùy chỉnh được gọi là clientarea và thiết lập một số cột tùy chỉnh cho nó trong khu vực quản trị - các cột tùy chỉnh là tất cả các trường meta tùy chỉnh, như bạn có thể thấy từ mã của tôi. Tôi muốn sắp xếp theo 'Ngày hẹn' giảm dần theo mặc định.
Tất cả các cột hoạt động tốt và có thể được sắp xếp theo cách thủ công như mong đợi, nhưng tôi không thể có thứ tự sắp xếp mặc định để hoạt động.
Nếu tôi thay đổi trường sắp xếp mặc định thành trường tiêu chuẩn (ví dụ: 'title') thì nó hoạt động như mong đợi; có vẻ như nó không hoạt động khi tôi đang cố gắng đặt một cột tùy chỉnh làm thứ tự sắp xếp mặc định. Thứ tự hoạt động (nghĩa là tôi có thể thay đổi giữa asc và desc theo mặc định ngay cả với một cột tùy chỉnh), nhưng nó không chọn thứ tự nên đang quay trở lại sắp xếp theo ngày bài đăng tùy chỉnh được xuất bản.
Tôi đang thiếu gì?
Mã của tôi là như sau:
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');
}
}