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 trialStephen Villabona
3,808 PointsShowing Custom Post type within another custom post type
I've created a few custom post types for my real estate page (i.e. Listings, Agents, & Neighborhoods). I'm having some trouble understanding how to display a specific agent post type within a single listing post type page.
Under my listing post type, I've created a few ACF fields, one being a checkbox or dropdown for agents. When an agent is selected from the checkbox / dropdown menu, I'd like it to recognize, which agent that is and display the information for that agent from the other custom post type of agents. I hope this makes sense.
I'm working in the single-listings.php and have a 2 column layout. The left column will have all the information about the real estate listing, address, pictures, description, etc. The right column, I want to display the agent picture, name, phone, & email based on who was selected when that custom listing was made.
I hope this makes sense!
2 Answers
Jacob Mishkin
23,118 PointsSince you are using ACF already, I would suggest using ACF's relationship field. This way you will be able to create a relationship between your custom post types. please see the docs below:
Stephen Villabona
3,808 PointsTo clarify more. I'd like to dynamically populate the information here depending on which agent was checked/selected when creating the listing.
<div class="listings-agent"> <h1>Listing Agent</h1> <img src="<?php this would be php code pulling featured image of selected agent ?>"> <h2><?php this would be php pulling Full Name of agent selected ?></h2> <h3><?php this would be php pulling phone number of agent selected ?></h3> <h3><?php this would be php pulling email of agent selected ?></h3> </div>
Keep in mind, that this information would be pulled from another custom post type other than the current page post type of "listings" that we would be on.
Jacob Mishkin
23,118 PointsWhat you would do is create a ACF field that corresponds to what data you want to be pulled into the page.
Stephen Villabona
3,808 PointsStephen Villabona
3,808 PointsThank you, I'll take a look. Just from a brief preview, it seems like it could be the solution. Do you have any experience with custom posts and fields? Is ACF good?
Stephen Villabona
3,808 PointsStephen Villabona
3,808 PointsHey Jacob, thank you for pointing me in the right direction. Because my case has only 1 correct option, I'm using post object instead of relationship, but I was successful in getting the item to show up by using the following code.
<?php $post_object = get_field('agent1');
?> <?php get_template_part( 'content', 'agents' ); ?> <?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?> <?php endif; ?>