This seems like something that should be very simple but I can’t seem to get it to work. I have a user relationship field that stores a single user. How do I go about getting specific values from that user such as the User ID or email address? Do you have to put it through a foreach loop for a single user field?
<?php $getcontacts = get_field('contact'); ?>
<?php foreach( $getcontacts as $getcontact ):
$getcontactid = $getcontact->ID;
echo $getcontactid;
endforeach; ?>
Thanks!
Pretty straight forward, heres how I usually do it.
$username = get_field('your_user_field_name'); // this gives us an array of all user data..
$userID = $username['ID']; // grabs the user ID
// this could be a custom ACF field you added to the user screen
$jobtitle = get_field('custom_user_field', 'user_'. $userID );
// grabs corresponding social info from user account
$theemail = get_the_author_meta('user_email', $userID);
$linkedin = get_the_author_meta('linkedin', $userID);
$twitter = get_the_author_meta('twitter', $userID);
$gplus = get_the_author_meta('googleplus', $userID);
You can look up get_the_author_meta for all of the other options available as well.
Edit: some screenshots.. This assumes you are using the actual “User” field type from ACF.