Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress

Adam Bowers
Adam Bowers
11,812 Points

Advanced Custom Fields | Post Object & linking a related custom post type including it's own custom fields.

How does one add the content of a related field type (http://www.advancedcustomfields.com/resources/post-object/) from the ACF ( Advanced Custom Fields Plugin ) to their single page template?

It's important to note that the post-object coming through is also a custom post type, with it's own related custom fields rather than the standard the_content(), the_title() etc.

Example Code:

<div class="type-service"> <?php while ( have_posts() ) : the_post(); ?>

            <img src="<?php the_field('service_image'); ?>"/>

            <h1><?php the_field('service_name');?></h1>
            <p><?php the_field('service_description'); ?></p>

</div>

        <aside class="content-right">
            <?php
                $post_object = get_field('related_study');

                if( $post_object ): 

                    // override $post
                    $post = $post_object;
                    setup_postdata( $post ); 
            ?>
                    <div>
                        <h3>Related Project</h3>
                        <h4><a href="<?php the_permalink(); ?>"><?php the_field('case_study_title'); ?></a></h4><br />
                        <img src="<?php the_field('case_main_image');?>" alt="" />
                    </div>
            <?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
            <?php endif; ?>

        </aside>
            <?php endwhile; ?>

A little explanation of the above perhaps: I'm getting the $post_object value by getting the post object as described in the link at the top, from a custom field called 'related study'

Then using the setup_postdata I was trying to access custom fields of the object that way.

Although it seems when I run a print_r($post) the containing array has the typical wordpress post properties. i.e the_title() but the post object doesn't contain any of that post types, custom fields?

Has anyone run into this before, do they have examples or suggestions of things I could try, or am I simply missing a trick here and I've gone wrong.

Any pointers would be helpful should you have time.

Adam Bowers
Adam Bowers
11,812 Points

I actually solved the riddle. It seemed that I had set the Custom Field type to Relationship under the 'Relational' option group. It should of actually been the Post Object type itself under the relational grouping.

It seemed the solve the issue. :)

1 Answer