Support

Account

Home Forums General Issues Query cpt then echo if relationship exists

Solved

Query cpt then echo if relationship exists

  • Hi there – have been stumbling around with this one for a while and can’t get it to work.

    I am querying a cpt (addons) and looping through ALL ITEMS outputting the ctp title. If a relationship exists with the current post I want to display a tick, if not display a cross.

    It’s returning the same for each ctp – since I want to show all cpts I assume that it’s not the query that’s wrong, but I’m not sure how to match this with the current post.

    If anyone could point me in the right direction would be grateful.

    <?php 
        $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 
    			$included = get_field('add_ons_suites'); ?>
    		    <?php if( $included ): ?><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; ?>  
    
  • You forgot to set the postdata in foreach loop.
    Just add this after the foreach line: setup_postdata($addon);

    Here is the doc: https://developer.wordpress.org/reference/functions/setup_postdata/

    That will set the $addon as the main $post for every iteration in foreach loop.

    As an alternative solution, you could also add the needed post_id in ACF’s get_field method like this:
    get_field(‘add_ons_suites’, $addon->ID)

    That will bring the acf field data for that specific post

  • Thanks aregmk – have added set_postdata($addon) after the foreach but not getting the right result, i.e. getting same result if item is in the relationship or not.

    Adding the post_id onto the add_on_suites gives me the opposite result. Note: add_on_suites is not a field of the addons cpt – it is a field on the current post (i.e. the relationship exists on this post to the addons cpt).

    Wondering if it could be the $included query <?php if( $included ): ?> is just checking if ‘anything at all’ is included and if so, then giving me a tick?

  • Hi renee, what does the add_on_suites field return? An array of the post or the post ID?

  • The post object – switched it to post ID but it makes no difference?

  • 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; ?>
  • Hi Aregmk – can see what you are getting at but it’s returning negative (i.e. <i class="fas fa-times"></i> where there are five add-ons selected.

  • I asked you what returns because we need to know what we have (post object or post ID) to do the relations between the addon and the current post.

    So the add_ons_suites is a relationship which contains a single value of the post ID or multiple values of posts IDs?

    And another question, the add_ons_suites is a field from the cpt addon or the current post?

    This questions will help me to give you a better resolution

  • Hi Luca,

    add_ons_suites is a relationship field on the current post – multiple posts from the ctp addons are selected and return the post-ID.

    Hope that helps.

  • 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; ?>
  • Thank you Lucas – that worked for 95% of them. However, when the page has no addons selected I get the following error:

    Warning: in_array() expects parameter 2 to be array, string given in

    Not sure what’s going on there?

  • Have tried the following and it’s removed the error but does not display anything if the page has no selected addons. If the array doesn’t exist then I can’t get it to output anything.

    <?php 
        if(is_array($addons_included_ids)) :
        if( in_array($addon->ID, $addons_included_ids) ): ?>
  • Try this

    
    <?php 
        if( !empty($addons_included_ids) && is_array($addons_included_ids) && in_array($addon->ID, $addons_included_ids) ): ?>
    
  • Thanks so much Luca – always have trouble with the relationship field 🙁

Viewing 14 posts - 1 through 14 (of 14 total)

The topic ‘Query cpt then echo if relationship exists’ is closed to new replies.