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.

    // 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">
    			<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>
    			// 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; ?>
    	<?php endforeach; ?>
    	<?php wp_reset_postdata(); ?>
    <?php endif; ?>