0

I try to sort a custom user field (team_user_id), I have all data but I can’t sort by numeric.

updated code

add_filter('manage_users_columns', [$this, 'custom_id_column']);
add_filter('manage_users_custom_column', [$this, 'custom_id_content'], 10, 3);

add_filter('pre_get_posts', [$this, 'sort_content']);

public function custom_id_column($columns)
{
  foreach ($columns as $k => $v) {
    if ($k === 'username') {
      $new_columns['ID'] = __('ID', 'default');
    }
    $new_columns[$k] = $v;
  }
  return $new_columns;
}

public function custom_id_content($value, $column_name, $user_id)
{
  $user = get_userdata($user_id);
  if ('ID' == $column_name) {
    return get_user_meta($user->ID, 'team_member_ID', true);
  }
  return $value;
}

public function sort_content($vars){
  if ( isset($vars['orderby'] && 'team_member_id' === $var['orderby']) {
    $vars = array_merge($vars, 
    array( 'meta_key' => 'team_member_id',
           'orderby' => 'meta_value_num' 
    ));
  }
  retunrn $vars
}

I tried in pre_get_post and request, but result is unsorted

3
  • Are you sure this code runs? It might help to paste the rest of your code, and what $vars looks like after the array_merge
    – mozboz
    Jul 21, 2020 at 15:22
  • updated code with full function create col, insert data col and sorting team_member_id is user meta key Jul 22, 2020 at 6:40
  • Resolve with add_filter pre_get_users Jul 23, 2020 at 14:07

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.