Support

Account

Home Forums General Issues User List in a Select Field

Solved

User List in a Select Field

  • I would like to create a select field with a list of registered users (use Display Name in list). I found the below code that creates a list of roles for the site which works great. I am not sure how to modify the code to get a list of registered users.

    add_filter('acf/load_field/name=user_groups', 'populateUserGroups');
    
    function populateUserGroups( $field )
    {	
    	// reset choices
    	$field['choices'] = array();
    	
    	global $wp_roles;
    	$roles = $wp_roles->get_names();
    	
    	foreach ($roles as $role) {
    		$field['choices'][ $role ] = $role;
    	}
    
    	return $field;
    }
  • I have not tested this, for more information see https://codex.wordpress.org/Function_Reference/get_users

    
    add_filter('acf/load_field/name=user_groups', 'populateUserGroups');
    
    function populateUserGroups( $field )
    {	
    	// reset choices
    	$field['choices'] = array();
    	
    	$users = get_users();
    	
    	foreach ($users as $user) {
    		$field['choices'][ $user->ID ] = $user->display_name;
    	}
    
    	return $field;
    }
    
  • Thanks John for the help. I also appreciate your support on these forums. I am new to ACF and your code and comments have been very helpful with my project.

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

The topic ‘User List in a Select Field’ is closed to new replies.