Support

Account

Home Forums Front-end Issues Problem with repeated objects assigned to posts

Solved

Problem with repeated objects assigned to posts

  • Hi,

    This time I got problem like this.

    I have a list of posts (tv packages). I also have different posts (premium canals), which I assing to the first posts by repeater field with post object field.

    For some reason premium canals, assigned to the latest tv package post, get repeated to other tv package posts (please see attached screens).

    The code I use.

    <div id="podstrona">
    				<div class="contentBodyWrapper kanalyPremium">
    				<?php
    				/* Główna pętla */
    				$args = array(
    					'post_type' => 'kanaly_premium',
    					'posts_per_page' => -1,
    					'orderby' => 'date',
    					'order' => 'DESC',
    				);
    				
    				$fullnewsloop = new WP_Query($args);
    				if($fullnewsloop->have_posts()): 
    				while($fullnewsloop->have_posts()): $fullnewsloop->the_post(); ?>
    
    						<div class="pakietBoxLista row">
    							<div class="col-xs-12 premiumTitle">
    								<h2><?php echo the_title(); ?>
    							</div>
    							<div class="col-xs-12 col-md-3 imgPrevBlock">
    								<?php if(get_field('okladka_pakietu_premium')): ?>
    									<?php $okladkaPremium = get_field('okladka_pakietu_premium'); ?>
    									<div class="headerImg">
    										<img src="<?php echo $okladkaPremium['sizes']['medium'] ?>" />
    									</div>
    								<?php endif; ?>
    							</div>
    							<div class="col-xs-12 col-md-9 shortDescBlock">
    								<?php if( get_field('krotki_opis_pakietu_premium')): ?>
    									<?php the_field('krotki_opis_pakietu_premium'); ?>
    								<?php endif; ?>
    							
    								<?php // check current row layout ?>
    									<?php if( have_rows('programy_lista') ): ?>
    										<?php while ( have_rows('programy_lista') ) : the_row(); ?>   
    											<?php
    											$post_objects = get_sub_field('kanal');
    											if( $post_objects ): ?>
    												<?php foreach( $post_objects as $post): // variable must be called $post (IMPORTANT) ?>
    													<?php setup_postdata($post); ?>
    														<li class="kanalTelwizyjny">
    															<section class="kanalWrapper">
    															<?php //Elementy kanału ?>
    															<?php if(get_field('klasa_obrazka_kanalu', $post->ID)): ?>
    																<div class="sprite sprite-premium sprite-premium-<?php the_field('klasa_obrazka_kanalu', $post->ID); ?>"></div>
    															<?php else: ?>
    																<div class="sprite sprite-premium sprite-premium-<?php echo $post->post_name; ?>"></div>
    															<?php endif; ?>
    															<div class="kanalWrapperInner row row-eq-height">
    																<div class="col-md-3 col-sm-4 imgPrevBlock">
    																	<?php if( get_field('pakiety_dostepne_w', $post->ID)): ?>
    																		<div class="dostepnyW"><p>Dostępne w pakiecie:</p><?php the_field('pakiety_dostepne_w', $post->ID); ?></div>
    																	<?php endif; ?>
    																	<?php if( get_field('dostepny_na_kanale', $post->ID)): ?>
    																		<div class="dostepnyNa"><p>Dostępny na kanale:</p><?php the_field('dostepny_na_kanale', $post->ID); ?></div>
    																	<?php endif; ?>
    																	<?php if( get_field('kategoria_kanalu', $post->ID)): ?>
    																		<div class="kategoriaKanalu"><p>Kategoria:</p><?php the_field('kategoria_kanalu', $post->ID); ?></div>
    																	<?php endif; ?>
    																	<?php if( get_field('jakosc', $post->ID) == "Jakość SD" ): ?>
    																		<div class="wyborJakosci"><p><?php echo get_field('jakosc', $post->ID); ?></p></div>
    																	<?php else: ?>
    																		<div class="wyborJakosci jakoscHD"><p><?php echo get_field('jakosc', $post->ID); ?></p></div>
    																	<?php endif; ?>
    																</div>
    																<div class="col-md-9 col-sm-8 shortDescBlock">
    																	<h3 class="catItemTitle">
    																		<?php the_title(); ?>
    																	</h3>
    																	<div class="catItemIntroText listaPremium">
    																		<?php the_content(); ?>
    																	</div>																				
    																</div>
    															</div>
    															</section>
    														</li>
    												<?php endforeach; ?>
    												<?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
    											<?php endif; ?>
    										<?php endwhile; ?>
    									<?php endif; ?>								
    							</div>
    							<div class="col-xs-12 col-md-12 readMore">
    							<a class="btn pull-right" title="więcej" href="<?php echo the_permalink(); ?>">więcej</a>
    							</div>
    						</div>
    						
    				<?php 
    				endwhile; endif;
    				wp_reset_query(); ?>
    				</div>
  • I just noticed that by commenting wp_reset_postdata (at the end of the loop) there is no repeating of boxes, but get_permalink( $post->ID ) does’t work, all 3 posts link to same single post.

    
    <?php endforeach; ?>
    <?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
    endif; ?>
    
  • Where is you code in relation to the main WP loop for this template. Is it inside that loop? Can you post all of the code for this template?

  • Looks like this

    <?php get_header(); ?>
    <div id="innerwrapper" class="page">
    	<div class="container">
    		<div class="row">
    			<div id="lewaKolumna" class="col-xs-3 col-sm-3 col-md-3 hidden-xs hidden-sm">
    				<div class="sideModule">
    					<?php if ( function_exists('dynamic_sidebar') && dynamic_sidebar(17) ) : else : ?>
    					<?php endif; ?>
    				</div>
    			</div>
    			<div id="prawaKolumna" role="main" class="col-xs-12 col-sm-9 col-md-9">
    			<div class="catHeader">
    				<div id="breadcrumbs">
    					<?php if(function_exists('bcn_display'))
    					{
    						bcn_display();
    					}?>
    				</div>
    				<div class="headerImg">
    					<?php if ( function_exists('dynamic_sidebar') && dynamic_sidebar(14) ) : else : ?>
    					<?php endif; ?>
    				</div>
    				<div class="headerCatDesc">
    					<?php if ( function_exists('dynamic_sidebar') && dynamic_sidebar(15) ) : else : ?>
    					<?php endif; ?>
    				</div>
    			</div>
    			<div id="podstrona">
    				<div class="contentBodyWrapper kanalyPremium">
    				<?php
    				/* Główna pętla */
    				$args = array(
    					'post_type' => 'kanaly_premium',
    					'posts_per_page' => -1,
    					'orderby' => 'date',
    					'order' => 'DESC',
    				);
    				
    				$fullnewsloop = new WP_Query($args);
    				if($fullnewsloop->have_posts()): 
    				while($fullnewsloop->have_posts()): $fullnewsloop->the_post(); ?>
    
    					<div class="pakietBoxLista row">
    						<div class="col-xs-12 premiumTitle">
    							<h2><?php echo the_title(); ?></h2>
    						</div>
    						<div class="col-xs-12 col-md-3">
    							<?php if(get_field('okladka_pakietu_premium')): ?>
    								<?php $okladkaPremium = get_field('okladka_pakietu_premium'); ?>
    								<div class="headerImg">
    									<img src="<?php echo $okladkaPremium['sizes']['medium'] ?>" />
    								</div>
    							<?php endif; ?>
    						</div>
    						<div class="col-xs-12 col-md-9">
    							<?php if( get_field('krotki_opis_pakietu_premium')): ?>
    								<?php the_field('krotki_opis_pakietu_premium'); ?>
    							<?php endif; ?>
    							
    							<ul id="pakietBoxLista">
    							<?php // check current row layout ?>
    								<?php if( have_rows('programy_lista') ): ?>
    									<?php while ( have_rows('programy_lista') ) : the_row(); ?>   
    										<?php
    										$post_objects = get_sub_field('kanal');
    										if( $post_objects ): ?>
    											<?php foreach( $post_objects as $post): // variable must be called $post (IMPORTANT) ?>
    												<?php setup_postdata($post); ?>
    													<li class="kanalTelwizyjny">
    														<section class="kanalWrapper">
    														<?php //Elementy kanału ?>
    														<?php if(get_field('klasa_obrazka_kanalu', $post->ID)): ?>
    															<div class="sprite sprite-premium sprite-premium-<?php the_field('klasa_obrazka_kanalu', $post->ID); ?>"></div>
    														<?php else: ?>
    															<div class="sprite sprite-premium sprite-premium-<?php echo $post->post_name; ?>"></div>
    														<?php endif; ?>
    														<div class="kanalWrapperInner row row-eq-height">
    															<div class="col-md-3 col-sm-4 imgPrevBlock">
    																<?php if( get_field('pakiety_dostepne_w', $post->ID)): ?>
    																	<div class="dostepnyW"><p>Dostępne w pakiecie:</p><?php the_field('pakiety_dostepne_w', $post->ID); ?></div>
    																<?php endif; ?>
    																<?php if( get_field('dostepny_na_kanale', $post->ID)): ?>
    																	<div class="dostepnyNa"><p>Dostępny na kanale:</p><?php the_field('dostepny_na_kanale', $post->ID); ?></div>
    																<?php endif; ?>
    																<?php if( get_field('kategoria_kanalu', $post->ID)): ?>
    																	<div class="kategoriaKanalu"><p>Kategoria:</p><?php the_field('kategoria_kanalu', $post->ID); ?></div>
    																<?php endif; ?>
    																<?php if( get_field('jakosc', $post->ID) == "Jakość SD" ): ?>
    																	<div class="wyborJakosci"><p><?php echo get_field('jakosc', $post->ID); ?></p></div>
    																<?php else: ?>
    																	<div class="wyborJakosci jakoscHD"><p><?php echo get_field('jakosc', $post->ID); ?></p></div>
    																<?php endif; ?>
    															</div>
    															<div class="col-md-9 col-sm-8 shortDescBlock">
    																<h3 class="catItemTitle">
    																	<?php the_title(); ?>
    																</h3>
    																<div class="catItemIntroText listaPremium">
    																	<?php the_content(); ?>
    																</div>																				
    															</div>
    														</div>
    														</section>
    													</li>
    											<?php endforeach; ?>
    											<?php // wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
    										<?php endif; ?>
    									<?php endwhile; ?>
    								<?php endif; ?>		
    							</ul>
    							
    						</div>
    						<div class="col-xs-12 col-md-12 readMore">
    							<a class="btn pull-right" title="więcej" href="<?php echo get_permalink( $post->ID ); ?>">więcej</a>
    						</div>
    					</div>
    					
    				<?php 
    				endwhile;
    				endif;
    				?>
    				</div>
    			</div>
    		</div>
    		</div>	
    	</div>
    </div>
    <?php get_footer(); ?>
    
  • I’m not seeing the main WP loop in this page. What type of a template is this? Is it for a single post of some kind, an archive or is it a custom page template?

  • This is archive’s custom template (archive-kanaly_premium.php) for custom post type. There is no other loop, I use that system for all other types of posts, pages and archives.

  • okay, whether or not you are doing another loop, there is a query that happens before yours. Any time you do a reset WP will reset to this query and not to the previous query and post whatever that may be. In essence what you have here is a loop that’s 3 deep of posts.

    I’ve boiled down your code here to the important bits.

    
    <?php
      // main WP query happens before here
      // Call it: Query #1
      
      // your query: Query #2
      $args = array(
        'post_type' => 'kanaly_premium',
        'posts_per_page' => -1,
        'orderby' => 'date',
        'order' => 'DESC',
      );
      $fullnewsloop = new WP_Query($args);
      if ($fullnewsloop->have_posts()): 
        while($fullnewsloop->have_posts()): $fullnewsloop->the_post();
          if( have_rows('programy_lista') ): 
            while ( have_rows('programy_lista') ) : the_row();
              $post_objects = get_sub_field('kanal');
              if( $post_objects ):
                foreach( $post_objects as $post): 
                  setup_postdata($post);
                endforeach;
                // this resets WP to main query: Query #1
                // and not to your query: Query #2 
                wp_reset_postdata();
              endif;
            endwhile;
          endif;
        endwhile;
      endif;
    ?>
    

    What you need to do instead of resetting is to store your post and then restore it after the your loop over $post_objects

    
    <?php
      // main WP query happens before here
      // Call it: Query #1
      
      // your query: Query #2
      $args = array(
        'post_type' => 'kanaly_premium',
        'posts_per_page' => -1,
        'orderby' => 'date',
        'order' => 'DESC',
      );
      $fullnewsloop = new WP_Query($args);
      if ($fullnewsloop->have_posts()): 
        while($fullnewsloop->have_posts()): $fullnewsloop->the_post();
          if( have_rows('programy_lista') ): 
            // temporarily store post from Query #2
            $post_hold = $post;
            while ( have_rows('programy_lista') ) : the_row();
              $post_objects = get_sub_field('kanal');
              if( $post_objects ):
                foreach( $post_objects as $post): 
                  setup_postdata($post);
                endforeach;
              endif;
            endwhile;
            // restore post from Query #2
            $post = $post_hold;
          endif;
        endwhile;
      endif;
    ?>
    

    However, after saying all of that, there isn’t any reason for your second query in the first place. Since this template is archive-kanaly_premium.php, that query is already being done by WP. What you should be doing is using a pre_get_posts filter https://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts to modify the post_per_page parameter of the main query. If you did this then your loops and reset would work correctly.

  • John, you guys are legends. That is just super support 🙂
    Cheers.

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

The topic ‘Problem with repeated objects assigned to posts’ is closed to new replies.