Support

Account

Home Forums ACF PRO Print user fields with values corresponding to that user

Solved

Print user fields with values corresponding to that user

  • Hi there,

    I have been trying hard to get this to work but can’t seem to get it functional. I am trying to print a list of all current users belonging to a certain role, and print information for those users that is supplied by a custom field. However, I can’t seem to get information to populate for that user, and can only get information to populate for the user currently logged in.

    I have been using the information on this documentation which does not work for me: https://www.advancedcustomfields.com/resources/how-to-get-values-from-a-user/

    Here’s what I have:

    <?php $other_user_id = get_the_author_meta('ID');
    $user_state = get_field('state', 'user_'. $other_user_id );
    
    	echo $user_state; ?>

    Can anyone tell me where I’m going wrong here?

  • You will have to supply more about he context you are using $other_user_id = get_the_author_meta('ID'); Is this inside the standard post loop?

  • Yes, inside the standard post loop. Currently, I am using it inside of a shortcode, although I have tried it inside of a template page too and can’t get that to work either.

    Here’s what my shortcode looks like:

      function print_members( $atts ) {
        		$args = array(
        'role'    => 'pmpro_role_2',
        'orderby' => 'last_name',
        'order'   => 'ASC'
    );
    
    $users = get_users( $args );
    $other_user_id = get_the_author_meta('ID');
    
    echo '<div>';
    foreach ( $users as $user ) {
    	echo '<div><h3>' . esc_html( $user->first_name ) . ' ' . esc_html( $user->last_name ) . '</h3>';
    	echo '<p>Email: <a href="mailto:' . esc_html( $user->user_email ) . '">' . esc_html( $user->user_email ) . '</a></p>';
    	echo '<p>Mailing Address: ';
    	echo the_field('address_line_1', 'user_'. $other_user_id );
    	echo '<br>';
        echo the_field('address_line_2', 'user_' . $other_user_id );
    	echo '<br>';
    	echo the_field('city', 'user_'. $other_user_id );
    	echo ', ';
    	echo the_field('state', 'user_'. $other_user_id );
    	echo'<p></div>';
    }
    echo '</div>';
    }
    add_shortcode('member-list', 'print_members');
  • It should be working unless there is something wrong with the global

    Can you try in the same place you are trying to get the state

    
    global $authordata;
    echo '<pre>'; print_r($authordata); echo '</pre>';
    

    and does it show the current user or the post author user?

  • When using the code you provided, it shows information for the post author user.

    I can return the post author (and current user) ID just fine, but when it’s supposed to be appended like the_field('state', 'user_' . $other_user_id ); it doesn’t seem to work. If I change ‘user_’ to any user ID in use (i.e. ‘user_3’), it returns the right information in that field.

  • I don’t know. Something is causing the function to return the wrong user ID so there is something else going on with the site and this is not caused by ACF. I don’t have an answer other than to look for plugin or theme conflicts.

  • This is a fresh install running TwentyTwenty-one theme. I have 2 other plugins running, but even with those disabled I can’t get it to function in this sense.

    Basically I’m seeing 2 issues here. The first being that I can’t get any user ID to work when used in the_field('state', 'user_' . $other_user_id ); because when I echo $other_user_id, it does give me *a* user ID, though maybe not the one that I want. When used in the_field('state', 'user_' . $other_user_id ); there is no output. The second issue being I’m trying to produce the user ID not for the page/post author, but for each user in order in that particular role type. If I can get either of these resolved in some manner, it’s going to get me closer to a solution.

    Any idea regarding the_field('state', 'user_' . $other_user_id );? Is there an instance of a usage like this that works for you or has been known to work?

  • Oh I think I cracked it!

    the_field('state', 'user_'.$user->ID ) passes along the user ID properly for the correct user, and it prints the information properly.

    I still have no idea why $other_user_id won’t pass the ID correctly, and unfortunately I don’t think I know enough about php to determine why either. But hopefully this solution will help someone else looking to do something similar.

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

You must be logged in to reply to this topic.