I have edited the default admin “Users” column to have two custom columns, “Number of Transactions” and “Latest Transaction” from a custom post type I created. I did not want to have the data that populates these columns as custom meta box fields as this seemed to create unnecessary options for the user and I did not want the user to be able to edit these fields.
After adding the columns I populated them with custom queries such as:
function test_modify_user_table_row( $val, $column_name, $user_id ) {
global $wpdb;
$user = get_userdata( $user_id );
$userID = $user->id;
$query_for_number_of_transactions = $wpdb->get_results(
"
SELECT COUNT(post_author) AS number_of_transactions FROM wp_posts WHERE post_type='club_orders' AND post_author=$userID;
"
);
$query_for_latest_transaction = $wpdb->get_results(
"
SELECT DATE_FORMAT((MAX(post_date)), '%M %e %Y') AS latest_transaction FROM wp_posts WHERE post_type='club_orders' AND post_author=$userID;
"
);
// Display the query data into custom columns
$er_qm_number_query_result = $query_for_number_of_transactions[0];
$er_qm_transaction_query_result = $query_for_latest_transaction [0];
switch($column_name){
case 'latest_transaction':
return $er_qm_transaction_query_result->latest_transaction;
break;
case 'number_of_transactions':
return $er_qm_number_query_result->number_of_transactions;
break;
default:
}
return "Error";
}
add_filter( 'manage_users_custom_column', 'test_modify_user_table_row', 10, 3 );
The above code works and displays the data I want to display in these columns. However, the issue I am running into is when I try to make either column sortable. I am finding a lot of material from Google searches pertaining to making custom columns populated by custom meta box fields sortable, but nothing on how to make a custom query populated columns sortable.
Is this even possible or do I have to create custom meta boxes and somehow make them read-only in order to sort this data?