TransWikia.com

Admin user column sort by numeric meta key

WordPress Development Asked by David Sticker on November 9, 2021

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

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP