Support

Account

Home Forums General Issues User fields doesn't work / how to use it

Solved

User fields doesn't work / how to use it

  • Hi ! Hope your summer is good 🙂
    There my question: how use “user field” ? I would like to display the username of the user.
    Thank you for your help.
    Best regards.
    math.

  • Hi Math.. Perhaps you’re calculating wrong *hohoho*

    In all seriousness, fetching a users username isn’t part of ACF but you can do it like this ( for the currently logged in user)

    
    <?php
    if(user_is_logged_in()){
    $currentuser = wp_get_current_user();
    echo $currentuser->user_login;
    }
    ?>
    

    EDIT: Sorry about the bad pun, I’ve had a bit of whisky..

  • 😉 Surely!

    Maybe I misspoke. These two different questions on the same topic:
    1) what is the “User Field” which allows a user to choose? How to retrieve the selection and as if she appears?

    2) In fact, for various reasons, I create a Custom Post Type “Author”. The template “Author” display the Related Posts. My skills are limited so that’s what I wanted to do because they are not Users who created the items, but the Admin:
    – Creation of the User;
    – Creation of the page for that User (-> Author)
    – In the Author, page selection in the fields User affiliate.
    – In the code view the posts created by this User / Author

    I hope you can help me, I tear a little hair there.

  • Hi @xmathx

    1. The user field (Core field type of the ACF plugin) is a simple select field which allows you to select one or more users. The data is saved as an array in the database of user ID’s.

    2. I don’t understand this question. Perhaps you could simple explain what you want to do, and how you intend ACF to work? Alos, explain what you have tried and what isn’t working

  • Thank you elliot for your reply.
    1. How to get the data?

    2. oups, my english is bad, so google translate from french to english 🙂
    If you explain me how to get the data (from the array) of the User Field, I think I can handle it.

  • if you’re in a post and want the author which you’ve set via the user field in ACF you can just retrieve it like normally in the loop:

    
    $userID = get_field('YOUR USER FIELD FIELDNAME');
    $theuser = get_userdata($userID); //This contains all the users basic info as an object
    
  • Thank you, but…

    I tried to recover the username but nothing is displayed.

    $userID = get_field('user2');
    $theuser = get_userdata($userID); 
    echo 'Username: ' . $theuser->user_login;
    

    I noticed with the code the_field(‘USER2’) showed that all of the information user.
    But with $user2 = get_field(‘USER2′) and echo $user2; nothing is displayed. Even with this code where it does not work:
    $user2 = ’20’; (id of a user) $theuser = get_userdata($user2) does not retrieve the value.
    So if I try $theuser = get_userdata(20); echo $theuser->user_login; it’s ok…

  • remove the ” around 20.. You want it as an integer and not a string.

    so $user2 = 20; should work..

  • Yes it works, but I want to get the id of get_field(‘ACF’); (from ACF User field) exactly like:

    $userID = get_field('user2');
    $theuser = get_userdata($userID); 
    echo 'Username: ' . $theuser->user_login;
  • 
    $userID = (int)get_field('user2');
    $theuser = get_userdata($userID); 
    echo 'Username: ' . $theuser->user_login;
    
  • This shows my username and not the user selected in the ACF User Field.

  • could you make sure that user2 is the correct fieldname and is this field on a regular post or something special?

    (int)get_field(‘user2’)
    says to retrieve the fieldvalue of fieldname user2 and turn it into an integer. So if the value is “20” it’ll be 20 as an integer.
    Then you use get_userdata which is a wp function and takes the users id which is correct..

    So there’s nothing wrong with the code, it has to be in your setup.

  • The field is located in a “post custom” type I created called Author. In the edit page, there are many in the “custom field” user2 = a number.

  • could you post print screens of your setup in ACF and in a author page (admin)

  • As the “custom field” displays the ID user2, perhaps it is enough to retrieve the value without using get_field but get_post_custom_value?

    Below the screenshot.

  • ahhh..

    You’ve confused wordpress regular meta boxes with ACFs..

    What you’ve done there is input the values in wordpress default meta boxes which will probably mess upp ACF.

    As you can see to the right you have the actual field! This is where you should set your author. delete those other..

    If you want to be able to set multiple authors you should have a look at the add on repeaterfield here on ACF and use that.

  • ok thank you !
    I may be a little abuse, but do you know exactly how to get this value “custom field”?

  • Im not sure what you want..

    If you remove those custom fields values that you created other than the field to the right it should work with the code I gave you.

    If you want to learn how to use the repeaterfield add on you can have a look here: http://www.advancedcustomfields.com/resources/field-types/repeater/

    It’s possible that you’ve messed up the field so if it still doesnt work you can try to rename the field like “user2_2” or something and try it again.

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

The topic ‘User fields doesn't work / how to use it’ is closed to new replies.