Support

Account

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; ?>