-1

Everything else works fine but it's still not sorting by number.

Would just like to know what I'm doing wrong here

Here's the code

 function add_user_columns( $defaults ) {
 $defaults['user_votecount'] = __('Votes', 'user-column');
 return $defaults;
 }

 function add_custom_user_columns($value, $column_name, $id) {
  if( $column_name == 'user_votecount' ) {
    return get_the_author_meta( 'wp__user_like_count', $id ); }

 add_action('manage_users_custom_column', 'add_custom_user_columns', 15, 3);
 add_filter('manage_users_columns', 'add_user_columns', 15, 1);

 function user_sortable_columns( $columns ) {
$columns['user_votecount'] = 'Votes';
return $columns;
 }

 add_filter( 'manage_users_sortable_columns', 'user_sortable_columns' );

 function user_column_orderby( $vars ) {
    if ( isset( $vars['orderby'] ) && 'user_votecount' == $vars['orderby'] ) {
    $vars = array_merge( $vars, array(
        'meta_key' => 'wp__user_like_count',
        'orderby' => 'meta_value_num',
        'order'     => 'asc'
    ) );
}
return $vars;
 }
 add_filter( 'request', 'user_column_orderby' );
2
  • For anyone with the same problem - discovered method on Wordpress support. see above edit Commented Nov 20, 2014 at 10:59
  • Please remove your solution from your question and post it as an answer and accept it. Thank you :-) Commented Nov 20, 2014 at 11:04

1 Answer 1

0

Instead of 'request' function, use this :

add_action('pre_user_query', 'user_column_orderby');
function user_column_orderby($userquery){
if('Votes'==$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 = 'wp__user_like_count' ";
$userquery->query_orderby = " ORDER BY alias.meta_value ".($userquery->query_vars["order"] == "ASC" ? "asc " : "desc ");
}
}
1
  • Please explain why this answer is helpful. You yourself have asked quite some questions. I'm sure you expected a better answer than this one, starting with formatting to explanation. Give the same chance to later readers who might need this answer.
    – kaiser
    Commented Nov 28, 2014 at 10:19

Not the answer you're looking for? Browse other questions tagged or ask your own question.