Displaying user data within ACF relational field (user type)

I'm trying to display user info including custom fields determined by an ACF relational user field, and all contained within a flexible content field.

EDIT Updated code following answers / comments, but still can't get anything to output - all my variables are empty.

The flexible content part is fine, and I'm just including it for context.

I have hit a brick wall with getting the related user info out though.

Within my flexible content layout, I have:

Field Type: User
Select multiple values?: Yes
Return format: User Array

Then in my template I have (most HTML Stripped out):

(role and author_image are ACF fields in the user entry).

<?php
$posts = get_sub_field('who_to_show');

  if( $posts ):

    foreach( $posts as $post):

      setup_postdata($post);

      $user_info = get_userdata($post);
      $image = get_field('author_image', 'user_' . $user_info->post_author );
      $role = get_field('role', 'user_' . $user_info->post_author );
      ?>

      <p><?php echo $role; ?></p>
      <img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>" />

    <?php endforeach;

  endif;
  wp_reset_postdata(); ?>

I also tried:

<?php
$posts = get_sub_field('who_to_show');

  if( $posts ):

    foreach( $posts as $post):

      setup_postdata($post);

      $image = get_field('author_image');
      $role = get_field('role');
      ?>

      <p><?php echo $role; ?></p>
      <img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>" />

    <?php endforeach;

  endif;
  wp_reset_postdata(); ?>

And still my variables are empty.

It's worth noting the image field Return Value is set to "Image Array" in ACF.

Any pointers in the right direction appreciated.

Answers 3

  • The solution was only slightly different from Lewis' answer:

    <?php
      $posts = get_sub_field('who_to_show');
    
      if( $posts ):
    
        foreach( $posts as $post):
    
            $userdata = get_user_by('ID',$post['ID']);
            $image = get_field('author_image', 'user_'. $post['ID'] );
            $role = get_field('role', 'user_'. $post['ID'] );
    
            echo $userdata->first_name;
            echo $role;
    
        endforeach;
    
        endif;
    
    wp_reset_postdata(); ?>
    

  • You don't actually need to call get_userdata(), all the data you need should be in the relational field array:

    EDIT: Add post ID and User ID to calls to get_field().

    <?php
    $posts = get_sub_field('who_to_show');
    
        if( $posts ): ?>
    
            <?php foreach( $posts as $post):
    
                setup_postdata($post);
                $user_info = get_field( 'relational_user_field', $post->ID );
                $image = get_the_field('author_image', 'user_'.$user_info->ID );
                $role = get_the_field('role', 'user_'.$user_info->ID );
                ?>
    
                <h3><?php echo $user_info->user_nicename; ?></h3>
                <img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>" />
    
            <?php endforeach;
        endif;
        wp_reset_postdata();
    
    endif; ?>
    

    Obviously swap 'relational_user_field' for your actual field name. The data returned in the user array is the same as the return values in the_author_meta


  • you wrote get_the_field, whilst the proper function to get acf data is get_field or the_field if you want to echo the stuff immediately.. i guess, this breaks your template.

    not sure, if i got you right, but if you added the field author_image to the user, than the following answer will get you there. if not, than my answer is crap :)

    the proper way of getting acf data from a user would be like this:

    get_field('author_image', 'user_' . $user_info->post_author );
    

    when you simply call get_field('parameter') it will return data from the current post, similar to get_field('parameter', get_the_ID()), so if you change that second parameter to whatever ID you have, you could get it from that specific ID.. in case of user it is user_(USER_ID)


Related Questions