Home › Forums › General Issues › Output Custom Fields Relational Post Object Field
I am using Custom Fields Relational Post Object Field. I would like to output the values with links using a shortcode.
Relational Post object field Name: related_posts
Example Output…
Related Posts: Related Post 1, Related Post 2, Related Post 3
Text before: Related Posts:
Text after each post except for last: ,
Each post that is related is a link to the post.
If no related posts then nothing outputs.
Example html…
<div class ="related-posts">
<span class ="text-before">Related Posts:</span>
<span class ="post"><a href="link to post">Related Post 1</a></span>
<span class ="post"><a href="link to post">Related Post 2</a></span>
<span class ="post"><a href="link to post">Related Post 3</a></span>
</div>
Example Output…
Related Posts: Related Post 1, Related Post 2, Related Post 3
Documentation for the post object field can be found here https://www.advancedcustomfields.com/resources/post-object/.
If there is something there that you do not understand let me know because it covers doing just what you want to do pretty well.
Hi John,
Thank you for the example link. Would I use Display list of posts (without setup_postdata) below?
I am not sure what exactly is setup_postdata?
<?php
$featured_posts = get_field('featured_posts');
if( $featured_posts ): ?>
<ul>
<?php foreach( $featured_posts as $featured_post ):
$permalink = get_permalink( $featured_post->ID );
$title = get_the_title( $featured_post->ID );
$custom_field = get_field( 'field_name', $featured_post->ID );
?>
<li>
<a href="<?php echo esc_url( $permalink ); ?>"><?php echo esc_html( $title ); ?></a>
<span>A custom field from this post: <?php echo esc_html( $custom_field ); ?></span>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
Since my Custom field is related_posts would I just change the below?
$custom_field = get_field( 'field_name', $featured_post->ID );
Change to…
$custom_field = get_field( 'related_posts', $featured_post->ID );
How can I add this all as a shortcode? I use code snippets plugin so I can create a snippet to add the whole. How can I put text before ” Related Posts:” What if there are no related posts will it return nothing?
If you are going to loop over the posts as in the code above then you do not need setup_postdata() because you’re using $featured_post->ID
This is for getting a field for the post of the related post, not the current post.
$custom_field = get_field( 'field_name', $featured_post->ID );
To add text above of below you can just add it inside of the if block.
Hi John:
Thank you for your response. Unfortunately I am not a PHP coder. Any chance you could put together this code?
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.