Wordpress - filter admin user list with custom fie

2019-06-10 08:27发布

I want to extend my view of users list by:

  • adding sortable column based on custom field
  • giving possibility of filtering list by custom field

So what i've did:

  • created custom field via ACF (its name is: opiekun_klienta)
  • entered proper values via user settings (so i've picked one of the values from the list)

I have such a piece of 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( $empty, $column_name, $opiekunklienta )
{
    $opiekunklienta = get_field( "opiekun_klienta" );
    if ( 'accountmanager_col' != $column_name )
        return $empty;

    return "<strong>$opiekunklienta</strong>";

the code adds column but there are no values displayed. Where is the mistake in the code? i cannot find it.. on top of that I would like to create a dropdown+submit button to filter the list in general (so the dropdown will display values of the custom field. I have no clue where to start with it..

thanks for any tips!

1条回答
Root(大扎)
2楼-- · 2019-06-10 08:49

Updated 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( $post_id, $column_name, $opiekunklienta )
{


$opiekunklienta_code = get_user_meta( $opiekunklienta,'opiekun_klienta',true); 

    if ( 'accountmanager_col' != $column_name )
    return $empty;    
    return "<strong>$opiekunklienta_code</strong>";
}

enter image description here

查看更多
登录 后发表回答