Home Forums General Issues User field type


User field type

  • Hi All

    This is driving me mad. I’ve a repeater with a user field type.

    If I use the_sub_field('select_staff'); I get an array of info on the user.

    How can I simply get the ID from that array?


  • The array it returns is the same as the array returned by the_author_meta. Thus, you can get any of the info stored therein (see Parameters on the linked page).

    First store the array as a variable, then select the ID.

    $select_staff = the_sub_field('select_staff');
    $select_staff_id = $select_staff('ID');
  • I’m looking to do the same thing and I can’t get it to work for the life of me. I’m trying to echo the ID from a user field and all I get is the whole array. I tried Jeremy’s code and it didn’t work for me. Anyone else have this issue?

  • Hi blueawesome

    You need to query the database, this article helped me:

    My code then had something like this:

    				$last = count($rows);	
    				$i = 1; 				
    				foreach( $rows as $row ) {				
    					preg_match('_([0-9]+)_', $row->meta_key, $matches);	
    					$meta_key = 'team_' . $matches[0] . '_select_staff';
    					$user_id = get_post_meta( $row->post_id, $meta_key, true );									

    Once I had the ID of the user, I could then do the following:
    <h5><?php $user_info = get_userdata($user_id); $first_name = $user_info->first_name; $last_name = $user_info->last_name; echo "$first_name $last_name"; ?><?php #echo ' - '.$user_id; ?></h5>
    I’ve no idea exactly what you’re trying to achieve but maybe the above will give you some pointers!

    jeremey’s code is useful but if you’re using a repeater then you need to go the query route I believe

  • Thanks for the response! I’ll try out your code in a bit. I’m just doing a very basic post query and I’m trying to echo the user ID from a user field to add to the $args to only show posts by the author specified.

  • Jeremy was on the right track for the simplest solution, but had a couple typos.

    FIXED code to get user ID:

    $select_staff = get_sub_field('select_staff');
    $select_staff_id = $select_staff['ID'];

    The User Relation field returns an array of data. Using get_sub_field* instead of the_sub_field* stores that array into the variable $select_staff.

    Then in order to retrieve the user ID, query the $select_staff array. Note the use of brackets for the array rather than the previous typo with parentheses.

  • Thanks for the reply mtv.jcil, but I’m still not getting this to work. I know it’s probably super simple but I can’t seem to make it work.

    I have a field on a custom post type that associates that post with a user. In my template file, I’m just trying to echo the ID of that user. Here’s a simplified version of what I’m using, just to test if it works.

    	$person = get_field('person_author');
    	$person_id = $person['ID'];	
    <p>ID: <?php echo $person_id[0]; ?></p>

    It’s not echoing anything though. Mine’s not in a repeater so I changed it to get_field.

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

The topic ‘User field type’ is closed to new replies.