Home Forums Front-end Issues Custom fields in repeater users


Custom fields in repeater users

  • I have Custom Post Type “Members” in which I have a repeater field populated with “Users” (That’s standard WP users), each of which have a couple of extra fields “Cell” and “Phone”.

    I’ve looked at the field instructions for showing user custom fields but don’t understand them and haven’t been able to get them to work.

    Could someone post a code example showing how to display these attributes please?

  • Hi @pbolger

    Just to clarify, are these “extra fields” attached to the user, or are they within the repeater field on each row?

    To load data from a user, you must find the user ID.
    You can find the ID like so:

    $user = get_sub_field(‘user’);
    $user_id = $user[‘ID’];

    Then to load any data from the user, you need to read this article:

    and do something like this:

    $cell = get_field('cell', "user_{$user_id}");


  • Hi Elliot,
    The extra fields are to extend the WP standard user. After doing a remake of a large local government site a few years ago I like to try and keep all ‘people’ in one place.
    After a bit of mucking around I got that to work. For the benefit of anyone else doing this here’s the code.

    <?php if(get_field('people')): ?>
    	<?php while(has_sub_field('people')): ?>
    		<ul class="person">
    			<?php $person=get_sub_field('person'); ?>
    			<?php $person_id=$person['ID']; 
    				$cell = get_field('cell', "user_{$person_id}"); 
    				$phone = get_field('phone', "user_{$person_id}"); 
    				echo $personid;
    			<li> <?php echo get_avatar( $email, $size, $default, $alt ); ?> </li>
    			<li class="persondetails">
    				<?php echo $firstname; ?> 
    				<?php echo $lastname; ?> 
    				<?php echo $email; ?> 
    				<?php if ($cell != '0'){ ?>Cell: 0<?php echo $cell; ?><?php } else {} ?> 
    				<?php if ($phone != '0'){ ?>Phone: <?php echo $phone; ?><?php } else {} ?> 
    				<?php the_sub_field('person=>user_lastname'); ?>
    				 <a href="<?php echo get_edit_user_link($person_id); ?>">edit person</a>
    	<?php endwhile; ?>
    <?php endif; ?>

    The number fields seem to strip leading zeros and default to zero. Not sure if there’s a more sensible way of dealing with phone numbers, but this seems to work ok for me.
    I wasn’t sure about using ‘user’ as a field name. It’s not a reserved word, but having multiple ‘user’ labels can make things pretty confusing.

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

The topic ‘Custom fields in repeater users’ is closed to new replies.