Support

Account

Home Forums Backend Issues (wp-admin) Admin users list – new sortable column based on custom field

Solving

Admin users list – new sortable column based on custom field

  • HI All!

    I’m struggeling with adding new column to the list view of users (users.php). At the very beginning i’ve added a relationship field (visible only for specific user roles) called opiekun_klienta (account_manager in english). It’s a dropdown list with few names.

    So far i have such code:

    add_filter( 'manage_users_columns', 'column_register_wpse_101322' );
    
    add_filter( 'manage_users_custom_column', 'column_display_wpse_101322', 10, 3 );
    
    function column_register_wpse_101322( $columns )
    {
    
        $columns['accountmanager_col'] = 'Opiekun';
        return $columns;
    }
    
     function column_display_wpse_101322( $value, $column_name, $user_id )
    {
      $user_info = get_user_meta( $user_id, 'opiekun_klienta', true );
      if($column_name == 'accountmanager_col') return $user_info;
      return $value;
    
    }

    and well, it more less works. More less = its adding column, displaying values BUT only as an ID (numeric). Not exact values like text. Why?

  • Sounds like the value stored in your ‘opiekun_klienta’ field is the user id of the account manager, so there’s just one more step to get the display name (or whichever field you want to show for the managers).

    I haven’t tested this, but this should get you close – replace the last function in your code above with:

     function column_display_wpse_101322( $value, $column_name, $user_id )
    {
      $manager_id = get_user_meta( $user_id, 'opiekun_klienta', true ); 
      $manager_name = get_user_meta( $manager_id, 'display_name', true);
      if($column_name == 'accountmanager_col') return $manager_name;
      return $value;
    
    }
  • Hi @lsell ,
    I have a similar thing to achieve. I used the @pawciak code to show a column in the dashboard’s users page that consists of an ACF text field:

    add_filter( 'manage_users_columns', 'column_register_acf_id' );
    
    add_filter( 'manage_users_custom_column', 'column_display_acf_id', 10, 3 );
    
    function column_register_acf_id( $columns )
    {
    
        $columns['id_col'] = 'ID';
        return $columns;
    }
    
    function column_display_acf_id( $value, $column_name, $user_id )
    {
      $user_info = get_user_meta( $user_id, 'czlonek_id', true );
      if($column_name == 'id_col') return $user_info;
      return $value;
    
    }

    czlonek_id is my ACf field name (location: user form > equal > add/edit). However this doesn’t work – the column shows up, but the value does not.
    Any advice? Thx.

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.