Home › Forums › General Issues › Query cpt then echo if relationship exists › Reply To: Query cpt then echo if relationship exists
If I understand you right, you have a related “addon” (one post of type “addon”) on this post through “add_on_suites” field.
If that’s correct, then you just need to check in every iteration which is the needed “addon” by adding a comparison operation like this: $related_addon_id == $addon->ID
Here is the code:
<?php $addons = get_posts(array( 'post_type' => 'addons', 'posts_per_page' => -1 )); ?> <?php if( $addons ): $related_addon_id = get_field('add_ons_suites'); ?> <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 if( $related_addon_id == $addon->ID ): ?><i class="fas fa-check"></i> <?php else: ?> <i class="fas fa-times"></i> <?php endif; ?> </td> </tr> <?php endforeach; ?> </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!
Are you building WordPress sites with ACF and @BeaverBuilder, and wanted to use your ACF Blocks in both the block editor and Beaver Builder?— Advanced Custom Fields (@wp_acf) May 10, 2023
The BB team recently added support for using ACF Blocks in Beaver Builder. Check it out 👇https://t.co/UalEIa5aQi
© 2023 Advanced Custom Fields.