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:

        $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">
    		<th colspan="2" scope="colgroup" class="text-uppercase">Product Inclusion</th>
    	      <?php foreach( $addons as $addon ): ?>
    		  <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>
    		    <?php if( $related_addon_id == $addon->ID ): ?><i class="fas fa-check"></i>
    		    <?php else: ?>
    		     <i class="fas fa-times"></i>
    		    <?php endif; ?>
                <?php endforeach; ?>
    <?php endif; ?>