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:
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 ofWP_Postobjects, 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$postitself, so template tags such asthe_title()andthe_permalink()only show the related post when the loop uses that global. - The
the_contentfilter formats the raw$post->post_contentthe waythe_content()does: it renders blocks, adds paragraphs and runs shortcodes. The echoed$outputis 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.
How to use get_the_content() with the related posts
With WordPress 5.2 or later, this shorter version needs no global $post and no reset:
$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,
nullandfalse, 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 withoutids, for example, shows the images attached to the main post instead of the related one. If the related posts contain shortcodes like that, use thesetup_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 likethe_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
$postis a number. Switch the field to Post Object, or use theget_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, makeif ( 'publish' !== get_post_status( $post ) ) continue;the first line inside theforeachof 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 onpost_contentto 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 customWP_Queryloop, call that query’s ownreset_postdata()method instead, becausewp_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.
Related articles
- listing hand-picked related posts from a Relationship field: the same field queried with WP_Query and post IDs
- reverse searching an ACF Relationship field: show the posts that link back to the current one
