Support

Account

Home Forums General Issues Loop through User Relationship Repeater Field

Solved

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">';

  • the break; statement in the first loop is causing it to only use the first user ID found. $user_ids[] will never have more than one element with that break in place.

  • Thanks John! That did it.

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

You must be logged in to reply to this topic.