Home › Forums › ACF PRO › Relationship field: how to dynamically populate landing page with CPT content
Hi there,
I’m having a problem: I’m trying to display content from a Custom Post Type post into a Landing Page using the relationship field.
This is the setup:
– there is a CPT called “movie” that has a lot of custom fields (director, year, actors etc).
– from time to time I need to create a landing page about a movie with more info (galleries, CTA, custom color combination) so I’ve some custom fields for this kind of template.
I don’t understand how can I use the relationship field to have the Landing Page populated with all the info from the selected movie. In this way I could use the Moive content as “main database” and add a landing only if/when needed with the ability to add all sort of editorial content.
Thanks,
Martino
Some of it depends on what you get from the relationship field, but all the basic code for using a relationship field is here https://www.advancedcustomfields.com/resources/relationship/. Exactly what is it you don’t understand?
I don’t understand if I can mix content from the page custom fields and from the CPT loaded via relationship field.
Should I have many WP_Query, one each time I need to extract a bit of info from the CPT? Then close it, reset the query to get the page custom fields and open it again and so on?
when you set up the relationship field it either returns post objects (for your cpt) or post ID values. All you need to do to get custom fields from the related post is to either use setup_postdata()
as explained on the page I linked to or supply the post ID when getting the field value, something like get_field('my_acf_field_name', $post->ID
)`
So I should store the CPT ID as a variable like this:
$ids = get_field('conference_talks', false, false);
and then get the custom fields like this:
get_field('my_acf_field_name', $post->$ids)
Right?
If your relationship field allows more than one value then it will return an array of ID’s and if you are only returning the IDs then you don’t need $post->
you only use this when using setup_postdata()`
$ids = get_field('conference_talks', false, false);
foreach ($ids as $id) {
$value = get_field('my_acf_field_name', $id);
}
All of this is explained in the documentation https://www.advancedcustomfields.com/resources/relationship/
The topic ‘Relationship field: how to dynamically populate landing page with CPT content’ is closed to new replies.
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 Privacy Policy. If you continue to use this site, you consent to our use of cookies.