Home › Forums › General Issues › Query cpt then echo if relationship exists › Reply To: Query cpt then echo if relationship exists
Cool, take a look at this and tell me if it works.
<?php
// this field contains an array of addons IDs
$addons_included_ids = get_field('add_ons_suites');
$addons = get_posts(array(
'post_type' => 'addons',
'posts_per_page' => -1
));
?>
<?php if( $addons ): ?>
<table class="table table-hover table-sm inclusions">
<thead>
<tr>
<th colspan="2" scope="colgroup" class="text-uppercase">Product Inclusion</th>
</tr>
</thead>
<tbody>
<?php foreach( $addons as $addon ): ?>
<tr>
<td>
<a href="<?php echo get_permalink( $addon->ID ); ?>">
<i class="fas fa-chevron-right mr-1"></i><?php echo get_the_title( $addon->ID ); ?></a>
</td>
<td>
<?php
// check if the current addon ID exists in the $addons_included_IDS array
if( in_array($addon->ID, $addons_included_ids) ): ?>
<i class="fas fa-check"></i>
<?php else: ?>
<i class="fas fa-times"></i>
<?php endif; ?>
</td>
</tr>
<?php endforeach; ?>
<?php wp_reset_postdata(); ?>
</tbody>
</table>
<?php 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!
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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.