-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' );
  • For anyone with the same problem - discovered method on Wordpress support. see above edit – tim daniels Nov 20 '14 at 10:59
  • Please remove your solution from your question and post it as an answer and accept it. Thank you :-) – Pieter Goosen Nov 20 '14 at 11:04
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 ");
}
}
  • 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 Nov 28 '14 at 10:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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