I am trying to add the ordering options to some custom user list columns I added in the Wordpress admin. This is my code :
function mmm_user_sortable_columns_query( $userquery ){
if( 'test_date' == $userquery -> query_vars['orderby'] ) {
global $wpdb;
$userquery -> query_from .= " LEFT OUTER JOIN $wpdb->usermeta AS alias ON ($wpdb->users.ID = alias.user_id) ";
$userquery -> query_where .= " AND alias.meta_key = 'test_date' ";
$userquery -> query_orderby = " ORDER BY alias.meta_value " . ($userquery -> query_vars["order"] == "ASC" ? "asc " : "desc ");
}
}
add_action( 'pre_user_query', 'mmm_user_sortable_columns_query' );
This doesn't sort properly, because my meta value is not a string but a DATETIME. So I tried to replace query_orderby with :
$userquery -> query_orderby = " ORDER BY CONVERT( datetime, alias.meta_value ) " . ($userquery -> query_vars["order"] == "ASC" ? "asc " : "desc ");
But that doesn't work. And adding
$userquery -> query_where .= " AND alias.meta_type = 'DATETIME' ";
doesn't do it either. What am I missing ?