Get Values From a User

Last updated Aug 25, 2023

Overview

All the template functions (get_field, the_field, etc) can be used to load values from a user, with a second parameter required to target the user. This is similar to passing through a $post_id parameter to target a specific post object.

The $post_id parameter needed is a string containing the word ‘user_’ and the user’s ID in the format; "user_{$user_id}".

Examples

Display a field

This example will display a field value from a user with an ID of 1.

<p><?php the_field('field_name', 'user_1'); ?></p>

Retrieving a field

This example will retrieve a field value from a user with an ID of 1.

<?php

$variable = get_field('field_name', 'user_1');

// Do something with $variable

?>

Working with the Repeater Field

Note that when inside of a have_rows loop, you do not need to use the $post_id parameter for any sub field functions (get_sub_field, the_sub_field)

<?php if( have_rows('repeater', 'user_1') ): ?>

    <ul>

    <?php while( have_rows('repeater', 'user_1') ): the_row(); ?>

        <li><?php the_sub_field('title'); ?></li>

    <?php endwhile; ?>

    </ul>

<?php endif; ?>

Author

This example will find the author for the current post, and display custom field data from that user.

<?php

$author_id = get_the_author_meta('ID');
$author_badge = get_field('author_badge', 'user_'. $author_id );

?>
<img src="<?php echo esc_url( $author_badge['url'] ); ?>" alt="<?php echo esc_attr( $author_badge['alt'] ); ?>" />

Related