Reputation: 93
I have 3 custom post types:
Each CPT have own Field Group in ACF as below:
All those fields are connected by relationship in ACF, so structure looks like this
John Doe -> Photography -> Location Shoots, Studio Shoots, Retouching
John Doe -> Videography -> Basic Filming, Drone Shoots, Postproduction
So, to explain this, one of the team members belongs to both groups Photography and Videography and he provides 6 services, 3 different services for each group.
Now, I have a few pages where I would like to output the team members data.
single-team-members.php
I'm outputting single team member data here, so on John's Doe card I have his Picture, Name, Groups he belong to (Photography, Videography) and all services he provides and this is done. A bit simplified while loop below:
while(have_posts()) {
the_post(); ?>
<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<h3 class="member-group"><?php
$relatedGroups = get_field('related_group');
if ($relatedGroups) {
echo '<ul class="member-group__list">';
foreach($relatedGroups as $group) { ?>
<li><a href="<?php echo get_the_permalink($group); ?>"><?php echo get_the_title($group); ?></a><span class="member-group__divider">, </span></li>
<?php
} echo '</ul>';
}?>
</h3>
<p><?php the_content(); ?></p>
<?php
$relatedServices = get_field('related_service');
if ($relatedServices) {
echo '<ul>';
foreach($relatedServices as $service) { ?>
<li><a href="<?php echo get_the_permalink($service); ?>"><?php echo get_the_title($service); ?></a></li>
<?php }
echo '</ul>';
}?>
<?php }
page-all-team.php
Here I have all team members cards where each card have outputted Picture, Name and the Group they belong to and here I outputted all groups they belongs to so for John is "Photography, Videography"
$relatedTeamMembers = new WP_Query(array(
'posts_per_page' => -1,
'post_type' => 'team-member',
'orderby' => 'title',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'related_team_group',
'compare' => 'LIKE',
'value' => array()
)
)
));
if ($relatedTeamMembers->have_posts()) {
while($relatedTeamMembers->have_posts()) {
$relatedTeamMembers->the_post(); ?>
<a href="<?php the_permalink(); ?>"><div class="team-member">
<div class="team-member__label-gold">
<div class="team-member__label-gold__name"><?php the_title(); ?></div>
<div class="team-member__label-gold__service">
<?php
$groups = get_field('related_team_group');
foreach($groups as $group){
echo $group->post_title;
}?>
</div>
</div>
</div></a>
<?php }
}
page-photography.php
On this page I would like to output:
page-videography.php
On this page I would like to output:
page-aerial-shooting.php
On this page I would like to output:
Upvotes: 1
Views: 1213
Reputation: 2534
When using the "post object" field of ACF, you will get a post object if you use the get_field
function.
You can use the get_field function with the ID of the post, to get the field you want of this specific post (targeted by the ID). So if you want to output a field called "myfield" , you can use the IDs you got in the "post object" field.
Please notice to use a correct foreach loop, using services
as service
(single).
$services = get_field('related_service');
foreach($services as $service):
$myfield = get_field('myfield', $service->ID); // using id of single post object
echo $myfield;
endforeach;
Upvotes: 1