I’ve actually managed to work this out myself. I used a different method, get_posts, and I also used meta_query to return an array and compare the page ID to the custom field. So my final code looked like this:
<?php $special_offers = get_posts(array( 'post_type' => 'special_offers', 'meta_query' => array( array( 'key' => 'applies_to', // name of custom field 'value' => '"' . get_the_ID() . '"', 'compare' => 'LIKE' ) ) )); if( $special_offers ): foreach( $special_offers as $offer ): // Do something to display the articles. Each article is a WP_Post object. echo "<div class='special_offer'><h3>"; echo $offer->post_title; // The post title echo "</h3><span class='offer_text'>"; echo $offer->post_content; // The post content echo "</span></div>"; endforeach; endif; ?>
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!
© 2023 Advanced Custom Fields.