Building on Phil's answer:
Made the following change to the switch:
case 'extranet_sort_document_case':
add_filter('posts_join', 'extranet_clientdocument_case_join');
add_filter('posts_fields', 'extranet_clientdocument_case_fields');
add_filter('posts_orderby', 'extranet_clientdocument_case_order');
break;
and wrote the following functions:
function extranet_clientdocument_case_join($join) {
global $wpdb;
$join .= "LEFT" LEFT JOIN wp_postmeta".$wpdb->postmeta." AS case_ids ON (wp_posts".$wpdb->posts.".ID = case_ids.post_id AND case_ids.meta_key = 'extranet_client_area')
LEFT JOIN wp_posts".$wpdb->posts." AS case_names ON (case_names.ID = case_ids.meta_value) ";
return $join;
}
function extranet_clientdocument_case_fields($fields) {
$fields.=", case_names.post_title";
return $fields;
}
function extranet_clientdocument_case_order($order_by) {
if(isset($_GET['order'])) $direction = $_GET['order'];
else $direction = 'ASC';
$order_by = 'case_names.post_title '.$direction;
return($order_by);
}
This worked in terms of making the column sortable by the post name of the related custom post, rather than by the metadata itself. However, it created a new problem in that the Title of the main post being listed, which is unusually the second column, now turned into the post title of the related post. I fixed this with the following additional code:
function restore_original_title($title) {
global $wpdb;
$post_id = get_the_ID();
$get_document_name = $wpdb->get_results('SELECT * FROM `wp_posts` WHERE `ID` = '.$post_id);
$title = $get_document_name[0]->post_title;
return $title;
}
and added the following additional filter:
add_filter( 'the_title', 'restore_original_title', 10, 2 );