Hey All,
I’ve created two CPT, one for events and one for locations. Each CPT has its own loop and single page template. Essentially we have a basic event calendar and store locator.
I’d like to let users select a branch location when creating an event. In doing so the event will pull in the branch’s name and address and display it on the event page. (This I have working.)
I’d also like to have each branch location display a list of upcoming events, which will be at that branch. (This is where I’m stuck)
I’ve connected the two CPT using the relationship field in ACF. The relationship field is inside a group field which contains 3 options, one of which is selecting the location. You can see the setup here: https://screencast.com/t/0EKw0GFPE7g
Currently my loop looks like this:
<?php
$bevents = get_posts(array(
'post_type' => 'event',
'numberposts' => 5,
'meta_query' => array(
array(
'key' => 'field_5c45422e00f3b', // name of custom field
'value' => '"' . get_the_ID() . '"', // matches exactly "123", not just 123. This prevents a match for "1234"
'compare' => 'LIKE'
)
)
));
?>
<?php if( $bevents ): ?>
<ul>
<?php foreach( $bevents as $bevent ): ?>
<li>
<a href="<?php echo get_permalink( $bevent->ID ); ?>">
<?php echo get_the_title( $bevent->ID ); ?>
</a>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
</div>
I think the problem is the location being a sub-field and I’m not selecting it correctly. However, I’m not sure. Any help would be really appreciated. Thanks!
Hi Byrd,
You’re right : Because of your ‘location’ data is in subfield you can’t query post by using it as meta_query (to be honest you can but by using an ugly solution).
I had the same issue with a repeater subfield and I got a very helpfull advice from John Huebner : See my post here.
To resume : Use the ‘save_post’ filter on your event CPT to save with add_post_meta the location as simple metadata (For example : ‘wp_location’). Then you’ll be able to easily query it.
The topic ‘Displaying relationship as a subfield on a CPT’ 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.