Support

Account

Home Forums General Issues User Field (Relational)

Solved

User Field (Relational)

  • I haven’t been able to find anything in the ACF documentation on the Relational User Field type. I am trying to use a repeater containing a single user field to allow the main admin of a blog to list contributors on their “about” page. The post editor allows me to select users from a dropdown menu as I expected but I am unsure how to then pull the metadata for display.

    Currently, a blunt <?php the_sub_field(‘author’); ?> returns all the metadata in a comma separated string. This can be seen here: http://wpc.matmartin.co.uk/info/

    What I’d like to be able to do is call the metadata selectively and in the order I choose, as I’ve been able to in the Author Posts template, here: http://wpc.matmartin.co.uk/author/ttf-master/

    Many thanks to anyone who can advise.

  • The user field returns an array of user information, similar to what the WP function get_users() does. I don’t think there has ever been documentation on the user field. You’ve got to do some experimenting to see what’s there.

    To see what is in the returned value use

    
    echo '<pre>'; print_r(get_sub_field('author')); echo '</pre>';
    

    to show the values you need to use

    
    $users = get_sub_field('author');
    

    and then show what you need. It might be a nested array, I’m not sure.

  • thanks john,

    this is super-useful, and as you can see here i’ve managed to pull what i need and style it accordingly as a result – for the record here’s the code that worked for me:

    <div class="infoauthorimg">
      <?php 
        $users = get_sub_field('author');
        echo get_avatar( get_the_author_meta( 'user_email' ), apply_filters( 'twentyeleven_author_bio_avatar_size', 120 ) );
    ?>
    </div>	
    <div class="infoauthorcopy">	
      <?php $users = get_sub_field('author'); ?>
      <h4><a href="#"><?php echo the_author_meta( 'display_name' ); ?></a></h4>
      <p class="nopad"><a href="<?php echo the_author_meta( 'user_url' ); ?>" target="_blank"><?php echo the_author_meta( 'user_url' ); ?></a></p>
      <p><a href="http://twitter.com/<?php echo the_author_meta( 'twitter' ); ?>" target="_blank">@<?php echo the_author_meta( 'twitter' ); ?></a></p>
      <p><?php echo the_author_meta( 'description' ); ?></p>	
    </div>

    so there are a couple of details which are a little beyond me – as you can see i’ve got a blank hyperlink there around the display_name – i’d like to be grabbing that author’s post archive url for there but it doesn’t seem to be in the array (see screenshot).

    i’d also really like to add some conditionals to the user_url and twitter data, so that they aren’t called as blank spaces if they don’t exist.

    any ideas on how i can fine tune this now?

    thanks so much – amazingly helpful as usual!

    m

  • Sometimes you’ll need to use the information you get with one of WPs functions. To get the author url you’d use $link = get_author_posts_url($users['ID']); https://codex.wordpress.org/Function_Reference/get_author_posts_url

    As far as checks

    
    if (get_the_author_meta('twitter')) {
      ?>
        <p>
          <a href="http://twitter.com/<?php 
              echo the_author_meta( 'twitter' ); ?>" target="_blank">@<?php 
              echo the_author_meta( 'twitter' ); ?></a>
        </p>
      <?php
    } // end if get twitter
    

    Also, I’m not sure, but you may need to supply the author ID in this case https://developer.wordpress.org/reference/functions/get_the_author_meta/ and https://codex.wordpress.org/Function_Reference/the_author_meta

  • thanks john,

    turns out i hadn’t gotten as far as i thought earlier – i’ve added a new user and alternated the calls in the repeater field to check the results but it seems i’m calling only one user all the time: http://wpc.matmartin.co.uk/info/.

    my guess is that i’m somehow calling the author of the page in question over the author specified in the sub field, but i can’t see how. have i missed something really obvious?

    here’s where my code is at for that part of the page template.

    <?php if( have_rows('author_profiles') ): ?>
      <div class="ca">
        <div id="pagebody">
          <h3>The Tap The Feed Team</h3>
          <?php while ( have_rows('author_profiles') ) : the_row(); ?>
          <div class="infoauthor">	
            <div class="infoauthorimg">
            <?php
              $users = get_sub_field('author');
              echo get_avatar( get_the_author_meta( 'user_email' ), apply_filters( 'twentyeleven_author_bio_avatar_size', 120 ) );
            ?>
          </div>	
          <div class="infoauthorcopy">	
            <h4 class="nopad"><a href="<?php $users = get_sub_field('author'); echo get_author_posts_url( get_the_author_meta( 'ID' ) ); ?>"><?php $users = get_sub_field('author'); echo the_author_meta( 'display_name' ); ?></a></h4>
            <p class="nopad"><?php the_sub_field('title'); ?></p>
            <p class="nopad"><a href="<?php $users = get_sub_field('author'); echo the_author_meta( 'user_url' ); ?>" target="_blank"><?php $users = get_sub_field('author'); echo the_author_meta( 'user_url' ); ?></a></p>
            <p><a href="http://twitter.com/<?php $users = get_sub_field('author'); echo the_author_meta( 'twitter' ); ?>" target="_blank">@<?php $users = get_sub_field('author'); echo the_author_meta( 'twitter' ); ?></a></p>
            <p><?php $users = get_sub_field('author'); echo the_author_meta( 'description' ); ?></p>	
          </div>
        </div>
        <?php endwhile; ?>
      </div>
    </div>
    <?php else : endif; ?>

    the role of each author (editor, sub-editor etc.) is a text field within the same repeater, and this is being called correctly, so i’m pretty sure the repeater is running as it should but i just haven’t targeted the author info within it, which would cause the code to fall back on the metadata for the page author i guess.

    as you can see, i’ve not added in those checks yet but your suggestion is pretty clear and makes sense to me. i’m assuming both for this and for the above (line 14) that if i have to specify the author ID in a changing repeater field then get_author_posts_url( get_the_author_meta( ‘ID’ ) ); will call it from the author in the sub field if used correctly?

    the trick is using it correctly, right?

    thanks again.

  • For all function inside the loop after getting the value of the field you need to either supply the correct user id or use a function that allows specifiying the user. For example https://developer.wordpress.org/reference/functions/get_the_author_meta/ the second parameter should be the user id you want to get the meta for. If you don’t supply the user ID then WP assumes you want the author of the post and not a specific one.

  • thanks john,

    i’m sorry but i really don’t understand – this isn’t because you’re not explaining it well, it’s because i don’t quite have the knowledge already. i can see from the link, and from this article (i think) what needs to happen but implementing the code is a little beyond me.

    i just can’t work out how to write the function that calls the ID of the user named in the sub_field “author”?

  • so, for example, here:

    
    <?php
              $users = get_sub_field('author');
              echo get_avatar( get_the_author_meta( 'user_email' , $users['ID']), apply_filters( 'twentyeleven_author_bio_avatar_size', 120 ) );
            ?>
    

    Once you have the user array you just need to use that array element wherever the WP function calls for the user ID. Or you can do this

    
    $user_id = $users['ID'];
    

    and then use that variable wherever you need to supply the user ID.

  • thank you so much john – this really worked for me, and as usual i have learnt something i’ll use again. really appreciate your persistence with this.

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

The topic ‘User Field (Relational)’ is closed to new replies.