Support

Account

Home Forums Front-end Issues I can’t get userdata to show properly from a User Object select field

Solving

I can’t get userdata to show properly from a User Object select field

  • I’m creating a company directory page, where I’m querying all users on he site and displaying their info. Some info is generated from ACF on user profiles. My user loop works great. I’m able to retrieve field data for some of my fields, however, I cannot get the User object field (Manager) to display properly. within ACF, i have this field set to return the User ID. Ultimately I just want the display name of the “Manager”.

    WHen trying to return the User Object, instead of the ID, it breaks the site.

    Within my user query, I’m using the following:

    
    $args = array('orderby' => 'display_name',);
    $users = get_users($args);
    foreach($users as $user){		
    	
    	$userID = 'user_'.$user->ID ;
    	
    	echo '<li>';
    		echo '<b style="font-size:14px;">' . $user->data->display_name . ' x' . $user->emp_office_ext . '</b><br/>';
    		echo '<em style="font-size:12px;white-space: nowrap; ">' . get_field('position',$userID) . '</em>';
    		
    		if (!empty(the_field('manager',$userID))){
    							
    			$managerID = get_field('manager',$userID);
    			$managerData = get_userdata($managerID);
    			$manager = $managerID->display_name;
    										
    			echo $manager;
    		}
    	echo '</li>';
    }
    
  • When changing the ACF for this user object to return the user array, I get all the data for this “manager’. But I can’t parse out just the display_name. I’ve tried this code with no luck

    $args = array('orderby' => 'display_name',);
    $users = get_users($args);
    foreach($users as $user){		
    	
    	$userID = 'user_'.$user->ID ;
    	
    	echo '<li>';
    		echo '<b style="font-size:14px;">' . $user->data->display_name . ' x' . $user->emp_office_ext . '</b><br/>';
    		echo '<em style="font-size:12px;white-space: nowrap; ">' . get_field('position',$userID) . '</em>';
    		
    		if (!empty(the_field('manager',$userID))){
    							
    			$manager = get_field('manager',$userID);
    										
    			echo $manager->display_name;
    		}
    	echo '</li>';
    }
  • Is your user field for a single select or does it allow multiple selections?

  • Then the user field is returning just a single user and not an array of users. See the “Display a single selected user” section of the doc.

  • I tried that as well, and have the same result

    $args = array('orderby' => 'display_name',);
    $users = get_users($args);
    foreach($users as $user){		
    	
    	$userID = 'user_'.$user->ID ;
    	
    	echo '<li>';
    		echo '<b style="font-size:14px;">' . $user->data->display_name . ' x' . $user->emp_office_ext . '</b><br/>';
    		echo '<em style="font-size:12px;white-space: nowrap; ">' . get_field('position',$userID) . '</em>';
    		
    		if (!empty(the_field('manager',$userID))){									
    			$manager = get_field('manager');									
    			echo $manager['display_name'];
    		}
    	echo '</li>';
    }
  • You do not need to loop over the users because the return value is a single user

    remove the loop

    
    foreach($users as $user){	
    

    use

    
    $user = get_users($args);
    if ($user) {
      
    }
    
Viewing 7 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic.