Wordpress - filter admin user list with custom field (ACF)

元气小坏坏 提交于 2019-12-08 12:37:17

问题


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:


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>";
}



来源:https://stackoverflow.com/questions/49250905/wordpress-filter-admin-user-list-with-custom-field-acf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!