Get values from another post

Overview

All the template functions (get_field, the_field, etc) can be used to load values from another post, however, a second parameter is required to target the post.

Each post has a unique ID which can be found in the URL when editing, or found via code such as $post->ID.

Examples

Display a field

This example will display a field value from the post with an ID of 123.

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

Retrieving a field

This example will retrieve a field value from the post with an ID of 123.

<?php

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

// do something with $variable

?>

Retrieving a field from post object

This example will retrieve a field value from a previously found $post object.

<?php

$variable = get_field('field_name', $post->ID);

// 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', 123) ): ?>

    <ul>

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

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

    <?php endwhile; ?>

    </ul>

<?php endif; ?>

Related

Hi all. Server maintenance scheduled this Sunday 28th May. Learn more