Get Post Content From an ACF Relationship Field

Quick Summary

  • Loop through the Relationship field's posts as the global $post, call setup_postdata() for each, print $post->post_content through the the_content filter, then call wp_reset_postdata().
  • Since WordPress 5.2, get_the_content( null, false, $post ) accepts a post object or ID, but it returns unformatted content, so wrap it in apply_filters( 'the_content', ... ).
  • the_content() and get_the_content() stop at a more tag and show the password form for protected posts; the filter on post_content always prints the full text.

To show the content of posts chosen in an Advanced Custom Fields (ACF) Relationship field, loop through the field’s posts as the global $post, call setup_postdata() for each one, and print the content with the_content() or by running $post->post_content through the the_content filter. Call wp_reset_postdata() after the loop. Since WordPress 5.2 you can also pass the post straight to get_the_content(), but it returns the content unformatted, so wrap it in the same filter.

Loop through the relationship field

This loop goes in the template that shows the post with the field, such as single.php. It works when the Relationship field, here related_posts, has its Return Format set to Post Object:

PHP
global $post;

$related_posts = get_field( 'related_posts' );

if( $related_posts ) :
    foreach( $related_posts as $post ) : setup_postdata( $post );

        // This worked for me
        $output = apply_filters( 'the_content', $post->post_content );
        echo $output;

    endforeach;
    wp_reset_postdata();
endif;
  • get_field( 'related_posts' ) returns the selected posts as an array of WP_Post objects, in the order you picked them.
  • The loop variable has to be the global $post. setup_postdata() sets up the post data that template tags read, but it doesn’t change $post itself, so template tags such as the_title() and the_permalink() only show the related post when the loop uses that global.
  • The the_content filter formats the raw $post->post_content the way the_content() does: it renders blocks, adds paragraphs and runs shortcodes. The echoed $output is ready to display.
  • wp_reset_postdata() puts the main post back once the loop ends, so the rest of the template shows the right title, ID and fields.

Why can’t you pass a post ID to the_content()?

the_content() has no post parameter. Its two arguments are the text of the “more” link and whether to strip the teaser, and it always prints the current post of the loop. That’s why the loop above sets up each related post first.

get_the_content() is different since WordPress 5.2: its third argument, $post, takes a post object or ID (see the get_the_content() reference). It still returns the content without the formatting filters, so blocks, paragraphs and shortcodes don’t render until you pass the result through the_content.

To load one post’s content by ID, get_post() and its post_content property also work; this WordPress Stack Exchange question covers more options. Reading data straight from the WP_Post object works for titles and other fields too.

With WordPress 5.2 or later, this shorter version needs no global $post and no reset:

PHP
$related_posts = get_field( 'related_posts' );

if ( $related_posts ) {
    foreach ( $related_posts as $related_post ) {
        echo apply_filters( 'the_content', get_the_content( null, false, $related_post ) );
    }
}
  • The first two arguments, null and false, keep the defaults for the “more” link text and the teaser.
  • It works with either Return Format, because get_the_content() accepts a post object or a post ID.
  • The main post is never replaced, so there is nothing to reset afterwards. The flip side: anything in the content that looks up the current post still sees the page’s main post. A [gallery] shortcode without ids, for example, shows the images attached to the main post instead of the related one. If the related posts contain shortcodes like that, use the setup_postdata() loop.

Which way of printing the content should you use?

The three options format the content the same way. They differ in how they treat a <!--more--> tag and password-protected posts:

  • the_content() inside the setup_postdata() loop shows only the part before a <!--more--> tag, followed by a “(more…)” link to the post. WordPress ignores the more tag only on the post’s own page and in feeds. For a password-protected post it prints the password form.
  • apply_filters( 'the_content', $post->post_content ), the code at the top, always shows the full text. It ignores the more tag and the post password, so it also prints the content of protected posts.
  • apply_filters( 'the_content', get_the_content( null, false, $post ) ) behaves like the_content(): the teaser with a “(more…)” link, and the password form for protected posts.

Use the first or third when your posts use more tags or passwords and you want those respected; use the filter on post_content when you want the whole text every time.

Common problems

  • Nothing prints and PHP warns “Attempt to read property “post_content” on int”. The field’s Return Format is Post ID, so $post is a number. Switch the field to Post Object, or use the get_the_content() version, which takes IDs.
  • Drafts or private posts show up. get_field() returns every post selected in the field, whatever its status, even to logged-out visitors. The field’s Filter by Post Status setting only limits what editors can pick, and a post can be unpublished after it was picked. To skip them, make if ( 'publish' !== get_post_status( $post ) ) continue; the first line inside the foreach of the loop above.
  • Only the start of a related post shows. The related post has a more tag and you print it with the_content(). Use the filter on post_content to show all of it.
  • The rest of the template shows the last related post. wp_reset_postdata() is missing. If this loop runs inside another custom WP_Query loop, call that query’s own reset_postdata() method instead, because wp_reset_postdata() always goes back to the main query’s post.
  • The page breaks when ACF is deactivated. get_field() no longer exists then. Check that ACF is active before calling its functions.

If you want editors to hand-pick related content like this without touching code, that’s the kind of build I do in custom WordPress development.

Share on X (opens in a new tab) Share on LinkedIn (opens in a new tab) Share on Facebook (opens in a new tab)

ChatGPT (opens in a new tab) Claude (opens in a new tab) Gemini (opens in a new tab) Perplexity (opens in a new tab) Grok (opens in a new tab)

More Insights

See More