Home › Forums › General Issues › Loop through User Relationship Repeater Field
I’m writing a shortcode plugin to display buddyboss user profile data on a CPT where multiple users ‘attached’ to the post are defined using an ACF User Relationship repeater field.
It’s working, but only showing in the first user defined in the repeater. It does not pull in subsequent users. I want it to display all users that are defined in the repeater.
// Define the shortcode
function bp_user_profile_shortcode($atts) {
// Get the post ID from the shortcode attribute
$post_id = isset($atts['post_id']) ? intval($atts['post_id']) : 0;
// Get the user ID from the ACF user relationship field in the repeater field
$repeater_field = get_field('exhibitor_rep', $post_id);
$user_ids = array();
if ($repeater_field) {
foreach ($repeater_field as $row) {
$user_relationship_field = $row['representative_user'];
if ($user_relationship_field) {
$user_ids[] = $user_relationship_field;
break;
}
}
}
// Loop through the user IDs and output the data for each user in HTML
$output = '';
foreach ($user_ids as $user_id) {
$user_data = bp_core_get_core_userdata($user_id);
$user_display_name = $user_data->display_name;
$user_email = $user_data->user_email;
$user_avatar = bp_core_fetch_avatar(array('item_id' => $user_id, 'type' => 'full'));
$user_custom_field_1 = xprofile_get_field_data('Title', $user_id);
$user_custom_field_2 = xprofile_get_field_data('Company', $user_id);
$user_custom_field_3 = xprofile_get_field_data('Biography', $user_id);
$user_custom_field_phone = xprofile_get_field_data('Phone', $user_id);
$user_profile_url = bp_core_get_user_domain($user_id);
// Output the data in HTML
$output = '<div class="bp-user-profile">';
if (!empty($user_avatar)) {
$output .= '<div class="avatar">' . $user_avatar . '</div>';
}
$output .= '<h3>' . $user_display_name . '</h3>';
$output .= '<strong>Email:</strong> ' . $user_email . '<br>';
if (!empty($user_custom_field_phone)) {
$output .= '<strong>Phone:</strong> ' . $user_custom_field_phone . '<br>';
}
if (!empty($user_custom_field_1)) {
$output .= '<strong>Title:</strong> ' . $user_custom_field_1 . '<br>' ;
}
if (!empty($user_custom_field_2)) {
$output .= '<p><strong>Company:</strong> ' . $user_custom_field_2 . '</p>';
}
if (!empty($user_custom_field_3)) {
$output .= '<p><strong>Biography:</strong> ' . $user_custom_field_3 . '</p>';
}
$output .= '<p><a href="' . $user_profile_url . '">View ' . $user_display_name . '\'s profile</a></p>';
$output .= '</div>';
}
return $output;
}
// Register the shortcode.
add_shortcode('bp_user_profile', 'bp_user_profile_shortcode');
Haven’t tested this but I’d look at your loop where you’re going through the user IDs. The first $output declaration is just:
$output = '<div class="bp-user-profile">';
Which would be fine if it was a single loop through it but every subsequent loop I think would get messed up by this, so the returned ID is maybe the last one in your field? It may be as simple as changing it to:
$output .= '<div class="bp-user-profile">';
You must be logged in to reply to this topic.
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’re hard at work on ACF 6.1, and Beta 1 is now available 🚀
— Advanced Custom Fields (@wp_acf) March 16, 2023
This release includes custom post type and taxonomy registration, an improved experience when selecting field types, PHP 8.1 and 8.2 compatibility, and more!
Let’s take a look 🧵https://t.co/Y0WcAT11l4
© 2023 Advanced Custom Fields.
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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.