I'm having trouble trying to display related post content and custom field content from one custom post type to another.

The Custom Post Types and posts are as follows:

- page title 1
- page title 2

- page title 1
- page title 2

CPT2 posts contain fields created using the Advanced Custom Fields plugin. CPT1 posts contains a Post Object Type field that I can link posts from CPT1 with the same title.

I've also set up a single-CPT1.php template file which I want to display all the content from the CPT1 posts, plus the linked content from the corresponding post in CPT2.

I've search various support pages on ACF for example https://www.advancedcustomfields.com/resources/post-object/ but I am still not able to get this to work.

So my question is how do I write the loop that pulls the corresponding post in CPT2 by the linked post object or by page title and display the custom field content from that post?

Please see my single-cpt1.php code


            *  Loop through post objects (assuming this is a multi-select field) ( don't setup postdata )
            *  Using this method, the $post object is never changed so all functions need a seccond parameter of the post ID in question.

            $post_objects = get_field('cpt2_related_post');

            if( $post_objects ): ?>

                <?php foreach( $post_objects as $post_object): ?>

                       <div><?php the_field('latest_activities', $post_object->ID); ?></div>

                <?php endforeach; ?>

            <?php endif; ?>
  • Can you post your single-CPT1.php code?
    – Den Isahac
    Commented Jun 26, 2017 at 15:41
  • I've just updated the question adding in the code form single-cpt1.php
    – Amesey
    Commented Jun 26, 2017 at 15:47
  • What specific problem you're experiencing from this code? Are you having trouble displaying the latest_activities?
    – Den Isahac
    Commented Jun 26, 2017 at 15:52
  • I can't display any of the content from the related post. 'latest_activities' is just one field that contains a text area.
    – Amesey
    Commented Jun 26, 2017 at 16:01
  • Try using var_dump inside the foreach loop, like so var_dump($post_object); if it actually returns a data. Also, does cpt2_related_post a repeater field?
    – Den Isahac
    Commented Jun 26, 2017 at 16:03


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.