Home › Forums › Add-ons › Repeater Field › How to access user data inside a repeater loop
I have a repeater field on a post called ‘episode_staff’.
Within this field, there is a sub field called ‘episode_staff_member’.
The subfield is a user field type.
So I add an ‘episode_staff’ field and then select a user in the episode_staff_member subfield. Each of those users has custom fields assigned to them, like ‘twitter_username’. I want to get the values of those custom fields.
Here is the code I have now, which prints the name of the user but I can’t get any of the custom fields to display.
<?php
if( have_rows('episode_staff') ):
while ( have_rows('episode_staff') ) : the_row();
$user = get_sub_field('episode_staff_member');
?>
<div class="masthead-single">
<?php foreach ($user as $user): ?>
<h4 class="masthead-name"><?php echo get_user_meta($user, 'first_name', true) ?> <?php echo get_user_meta($user, 'last_name', true) ?></h4>
<div class="masthead-title"><?php echo get_sub_field('twitter_username', $user) ?></div>
<?php endforeach; ?>
</div>
<?php endwhile;
endif; ?>
Any tips?
I believe you can get the user ID by using this code:
$user_id = $user->ID
With that user ID, you can get the custom field attached to a user. Please take a look at this page to learn more about it: https://www.advancedcustomfields.com/resources/how-to-get-values-from-a-user/.
I hope this helps.
Hi @acf_support — thanks for the reply.
I changed my code to this, but still no luck:
<?php
if( have_rows('episode_staff') ):
while ( have_rows('episode_staff') ) : the_row();
$user = get_sub_field('episode_staff_member');
?>
<div class="masthead-single">
<?php foreach ($user as $user):
$user_id = $user->ID ?>
<h4 class="masthead-name"><?php echo get_user_meta($user, 'first_name', true) ?> <?php echo get_user_meta($user, 'last_name', true) ?></h4>
<div class="masthead-title"><?php echo get_sub_field('twitter_username', 'user' . $user_id) ?>
</div>
<?php endforeach; ?>
</div>
<?php endwhile;
endif; ?>
I believe it is a problem with the loop, as the foreach loop is cycling through 10 times and there are only 2 episode_staff rows…
Okay, never mind. I figured it out. Here is my working code in case anyone has a similar problem.
<?php
if( have_rows('episode_staff') ):
while ( have_rows('episode_staff') ) : the_row();
$user_id = get_sub_field('episode_staff_member')[ID];
?>
<div class="masthead-single">
<h4 class="masthead-name"><?php echo get_sub_field('episode_staff_member')['user_firstname'] ?> <?php echo get_sub_field('episode_staff_member')['user_lastname'] ?></h4>
<ul>
<li><?php echo get_sub_field('episode_staff_member')['user_email'] ?></li>
<li><?php echo get_field('twitter_username', 'user_'. $user_id) ?></li>
</ul>
</div>
<?php endwhile;
endif; ?>
The topic ‘How to access user data inside a repeater loop’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.