0

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.

  • so basically you want to get 'author_image' from $user_info, right? – honk31 Aug 30 '18 at 16:39
  • I want to get info from get_userdata, as well as a custom field (text) and custom field (image). None of them work, so I have got something fundamentally wrong.. – user2265915 Aug 30 '18 at 16:45
1

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

  • Thank you but I was using get_userdata() in an attempt to get the standard user information rather than ACF fields. I tried adapting your answer but my variables are empty. Question updated with my new attempts. – user2265915 Aug 31 '18 at 9:49
  • 1
    It looks like you're passing a Post ID to get_userdata() but you need to pass a User ID. – Lewis Donovan Aug 31 '18 at 10:22
  • Ok, I'm taking what I can from the ACF documentation but that doesn't provide examples for the "User" field type in this context. Would you be able to update your answer / point me to some documentation that might help? – user2265915 Aug 31 '18 at 16:21
  • Sure thing - if you read the ACF docs on get_field() you'll see a section called 'Get value from different objects' which explains the formatting required for getting field values from various object types. The format required for user objects is 'user_1' (where '1' is the user ID). I've updated my code above to reflect this. – Lewis Donovan Sep 3 '18 at 10:59
0

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)

  • Changing get_the_field to get_field stopped my template from breaking thank you. I still can't get any data though. I updated question with my new attempts. – user2265915 Aug 31 '18 at 9:45
  • @user2265915 so you created 2 acf fields author_image & role and added them to location user form is equal to all. then you added content to these fields in the user accounts. then you call these users inside some repeater/flexible. all correct till this point? – honk31 Aug 31 '18 at 16:30
  • Yes, though I used user role is equal to all rather than user form. I call these users inside a [relational user] field, which is within a flexible content field. – user2265915 Aug 31 '18 at 18:10
  • i once had the same setting you are using, but then i switched it to the one i mentioned in my comment, since the other one was having a bug that i reported. not sure if it is fixed by now, i strongly believe it, but the answer from the dev guy was: use that setting until its fixed. this settings works for me, so there was no need for me to switch it back. please try.. change the field-settings and update the user one more time. – honk31 Sep 3 '18 at 7:23
0

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(); ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.