How to Add Custom ACF Fields to User Columns in WordPress
In this tutorial I will show you the code you need to add in your functions.php in order to add new acf fields to user columns in wordpress admin panel. The best part here is after you add the new column in users listing, you will be able to search by this field too.
Add ACF Fields To User Columns
First of all we have to add the new column in thead. To do that add the following code to your functions.php.
function add_acf_columns ( $columns ) { return array_merge ( $columns, array ( 'new_column' => __ ( 'My new column' ), ) ); } add_filter ( 'manage_users_columns', 'add_acf_columns' );
Now, we have to add the acf value for every single user in its column. To do that add the following bunch of code to your functions.php, below the add_acf_columns() function.
function exhibition_custom_column ($val, $column, $user_id ) { switch ( $column ) { case 'my_column': $my_column = get_field('column', 'user_'.$user_id); $my_column_post = get_post($my_column); if(isset($my_column_post)){ return $my_column_post->post_title; }else{ return ''; } break; } return $val; } add_action ( 'manage_users_custom_column', 'exhibition_custom_column', 10, 3 );
As you can see in the second function, it checks every key of the $column, when it find the column from our first function (my_column) we add our code. You can use this code to add any type of column you want. Let me know if you need any help with this functions in the comments section.