Support

Account

Home Forums Front-end Issues the_field and get_field is not working in get_users loop

Solved

the_field and get_field is not working in get_users loop

  • So I have been trying all night to get my front-end user query to work.

    I have the loop working, and can display default user data like “display_name” easily, but when I try to add custom user meta added by ACF “org_name” it doesn’t show anything. I have linked to screenshots of all of my code/settings. Any idea what I need to do?

    HTML created by current code
    HTML created by current code

    My current code
    My current code

    My organization fields
    My organization fields

    Proof that I have data saved in the test user
    Proof that I have data saved in the test user

  • Hey man!

    You must use something like that:

    $user_id = get_current_user_id();
    get_field("key","user_".$user_id);
    the_field("key","user_".$user_id);
  • Hey, @vverner, thanks for the reply! I added your code (not sure if it’s in the right place though ) but nothing happened. The code below still just outputs the display_name but not the org_name field.

    Is it in the wrong place? What else am I doing wrong?

    
    <?php
    
        $args = array(
            'role'         => 'organization',
            'orderby'      => 'login',
            'order'        => 'ASC',
        );
        $org_users = get_users( $args );          
        foreach ($org_users as $user) {
            $user_id = get_current_user_id();
            get_field("org_name","user_".$user_id); 
    
            echo '<div class="resource-item">';
            echo '<p>';
            echo esc_html( $user->display_name );
            echo the_field("org_name","user_".$user_id); 
            echo '<br>';
            echo '</p>';
            echo '</div>';
        }
    
    ?>
    
  • You can’t use “get_current_user_id()” inside the loop, sorry about that, try this instead:

    foreach ($org_users as $user) {
    $user_id = $user->user_id
    [.....]
  • Boom thanks @vverner!

    it ended up being:

    
    $user_id = $user->ID;
    

    not

    
    $user_id = $user->user_id
    

    But yours got me there! Thanks!

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

The topic ‘the_field and get_field is not working in get_users loop’ is closed to new replies.