Support

Account

Home Forums ACF PRO Exclude Current Post in Foreach

Solving

Exclude Current Post in Foreach

  • Hello, Still I’m trying to exclude Current post in Related post lists from other post fields with foreach condition, i couldn’t solve this problem,

    I have 7 different CPT, One of CPT is COMMON for other 5 CPTs and one of is Glossary to save terms in Glossary as a fact to assign facts into MAIN CPT from glossary (I know little bit complicated).

    for example:

    When I’m adding for example MAIN BRAND, I have many relationship fields in MAIN CPT part linking from glossary and other sub CPTs. And mostly fields are assigning(or adding) from GLOSSARY into MAIN CPT. In this logic, I’m using MAIN BRAND part for Library of sub products to bring common informations, and I’m using GLOSSARY part for assigned informations as a term with explanation and I’m bringing terms picture or short sums into Sub products via MAIN BRANDS.

    For Example

    -Owner -< COMES FROM GLOSSARY for example y OWNER post at GLOSSARY in OWNER CATEGORY at GLOSSARY,
    -Product Offers -< COMES FROM OTHER POSTS,

    owner are assigning in MAIN CPT from glossary as an owner.
    products are adding in Main brand or When making sub products, Main brand is selecting in product offers field (cos this is bidirectional fields. MAIN BRAND and SUB PRODUCTS are using same name custom fields to related between each together)

    <?php 
    $posts = get_field('product_offers_relationship');
    if( $posts ): ?>
      <?php foreach( $posts as $post): // variable must be called $post (IMPORTANT) ?>
        <?php setup_postdata($post); ?>
        <?php if( (get_field('if_product_offers_description_has', $post->ID) == 'yes') or (get_field('product_offers_relationship', $post->ID)!= "")): ?>
          <div class="main-content-list">
    <!-- Owners From Product Offers Main Brand -->
            <h3>Product Offers & Software Platforms</h3>
            <div id="pryc-wp-acctp-original-content">
              <?php if (get_field('if_product_offers_description_has', $post->ID) == 'yes') { ?>
              <?php echo get_field('product_offers_description', $post->ID); ?>
              <?php } elseif (get_field('if_product_offers_description_has', $post->ID) == 'no') { ?>
              <?php } ?>
              <?php
              $posts = get_field('product_offers_relationship', $post->ID);
              if( $posts):?>
    
                <div id="products" class="row view-group">
                  <?php foreach( $posts as $post): // variable must be called $post (IMPORTANT) ?>
                    <?php setup_postdata($post); ?> 
    
                      <div class="item grid-group-item brand-relationship col-xs-4 col-lg-3-brand">
                        <div class="thumbnail card">
                          <div class="img-event">
                            <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" >
                             <?php get_template_part( 'lib/factlist_thumbclass', get_post_format() ); ?>
                            </a>
                          </div>
                          <div class="caption card-body text-center">
                            <h6 class="group card-title inner list-group-item-heading">
                              <?php the_title_attribute(); ?>
                            </h6>
                            <div class="row">
                              <div class="col-xs-12 col-md-12-brand">
                                <a href="<?php the_permalink(); ?>"><button type="button" class="btn btn-brand panel-button">Review</button></a>
                              </div>
                            </div>
                          </div>
                        </div>
                      </div> 
                   
                  <?php endforeach; ?>
                </div>
              <?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
              <?php endif; ?>
              <hr>
            </div>
          </div>
        <?php else: ?>
        <?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
        <!-- No Content -->
        <?php endif; ?>
      <?php endforeach; ?>
      <?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
    <?php endif; ?>   

    At this part, I’m getting product offers list from main brand if sub product is assigned, then I’m bringing ‘Main brands other related products’ list how many other products added into MAIN BRAND. afte than I’m showing related products at SUBPRODUCT CPT1 frontend. There is no any problem at this part. I’M listing all related sub cpt at frontend. I just want to stop to showing CURRENT post in list at inside as a related posts from MAIN CPT offers cos

    SUBPRODUCT CPT1 [‘product_offers_relationship] = MAIN BRAND //showing
    MAIN BRAND CPT [‘product_offers_relationship’] = SUBPRODUCT CPT1, SUBPRODUCT CPT2, SUBPRODUCT CPT3

    when I’m bringing MAIN BRAND CPT list, it is showing

    SUBPRODUCT CPT1, SUBPRODUCT CPT2, SUBPRODUCT CPT3

    However I have to disable showing SUBPRODUCT CPT1 at this list beacuse also I’m on current page. How i will do it still I can not find any sollution.

    If anyone could help me, I would be very thankful

  • Before looking at the problem of excluding the current post, as it stands your nested loops cannot work.

    The reason is that wp_reset_postdata() always resets $post to the current post in the main WP query, it does not reset $post to a previous value. This is not stressed nearly strong enough in any WP documentation, not to mention that it’s not covered very well by people that have blogged about dealing with it.

    You cannot use setup_postdata() and wp_reset_postdata() beyond a secondary query.

    With nested post loops beyond the secondary you must access the post and get information about the post in a different way. For example:

    
    if ($third_nesting_posts) {
       foreach ($third_nesting_posts as $third_nesting_post) {
          $title = get_the_title($third_nesting_post->ID);
          $value = get_field('field_name', $third_nesting_post->ID);
       }
    }
    

    Also, another issue is that you’re using the same variable for multiple calls in nested loops.

    
    $posts = get_field('field_name');
    foreach ($posts as $post) {
      // this overwrites the value of the array you're looping over
      $posts = get_field('field_name', $post->id);
    }
    

    The second time through the loop you’ll actually be looking at the second post from the second time you got the field value and not the second value of the original array.

  • Hello John

    Thank you to reply it

    Now my code is clear and i think this more clean then first

    <?php
    
    $mainbrandquery = get_field('product_offers_relationship');
    
    // go related MAIN POSTS and start searching query in mainbrands
    
    if($mainbrandquery): ?>
      <ul class="list-unstyled">
    
        <?php foreach( $mainbrandquery as $m) : ?>
    
          <?php 
          $mainbrandsowner = get_field('owner_facts_post_relationship', $m->ID);
    
          // go and search owner posts at mainbrand
          
          ?>
    
          <li>
    
            <?php foreach( $mainbrandsowner as $o) : ?>
    
              <?php 
              $mainbrands = get_field('owner_facts_post_relationship', $o->ID);
    
              // we are in owner and check how many other main brands assigned in this field
              
              ?>
    
                <?php foreach( $mainbrands as $m) : ?>
                  <?php 
                  $subproducts = get_field('product_offers_relationship', $m->ID);
    
                   // find sub products at each assigned mainbrands and start listing them on front end
                  
                   ?>
                    <?php foreach( $subproducts as $s) : ?>
    
                    <!-- NOW SUB POSTS OF MAINBRANDS WITH SAME OWNERS ARE LISTING HERE -->
    
                    <!-- system need to show SUB PRODUCTS of MAINBRANDS which assigned with same owner exclude current post -->
    
                    <!--This is listing like
    
                      - Product a
                      - product b
                      - product c ( c is also current post )
    
                    -->
    
                    <p><?php echo get_the_title($s->ID); ?></p>
                    <p><?php echo get_the_permalink($s->ID); ?></p>
    
                    <!-- NOW SUB POSTS OF MAINBRANDS WITH SAME OWNERS ARE LISTING HERE -->
    
                   
    
                    <?php endforeach; ?>
    
                <?php endforeach; ?>
    
             <?php endforeach; ?>
           </li>
        <?php endforeach; ?>
      </ul>
    <?php endif; ?>

    at this point, still I’m trying to find solution to exclude it current post in list when i will find the solution, i will share it or i have to wait someone to help this situation 🙂

  • You need to store something about the posts you want to check on an exclude. For example the post ID of the main post.

    
    // before your custom query
    $main_post_id = $post->ID;
    
    foreach ($mainbrandquery as $m) {
       if ($m->ID == $main_post_id) {
         // skip this one
         continue;
       }
       // nested loop
       foreach ($subproducts as $s) {
         if ($m->ID == $s->ID || $s->ID == $main_post_id) {
           // skip this one
           continue;
         }
       }
    }
    
Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.