Home Forums Front-end Issues Checking custom posts in repeater loop breaks the repeater


Checking custom posts in repeater loop breaks the repeater

  • Hi,

    I’ve been looking for a bug in my code for quite some time, so I decided to call for help to double check it by You guys 🙂

    What I am trying to do is to list custom posts (schedule), each with a repeater containing elements (date and custom post id).

    Then I am checking each repeaters element against it’s object ID, using wp_query and a loop. I am looking for assigned AD image to that custom post’s ID.

    Once Ad image is being found, repeater’s loop stops, instead of going to the last element in it.

    I’ve tried different resets in different places with no luck.


    12:00 – AD image found
    stops and breaks loop.

    The code

    <div id="scheduleList" class="swiper">
    				<div class="swiper-wrapper">
    					<?php while($scheduleloop->have_posts()): $scheduleloop->the_post(); ?>
    						<div class="swiper-slide">
    							<?php if(have_rows('date_rep')): ?>
    								<?php while(have_rows('date_rep')) : the_row(); ?>
    									<?php //Body inner
    									    include( locate_template( 'template-parts/podcast-schedule-2-include.php', false, false ) );
    								<?php endwhile; ?>
    						<?php endif; ?>
    				<?php endwhile; ?>

    Included file

    <?php //ACF
    	$program_id = get_sub_field('program_id');
    	$schedule_title = get_field('program_title', $program_id);
    	$schedule_desc = get_field('program_desc', $program_id);
    	$author_names = get_field('author_names', $program_id);
    	$schedule_img = get_field('program_icon', $program_id);
    	$start_hour = get_sub_field('start_hour');
    	$program_old_slug = get_permalink($program_id);
    <div class="podcastElement pt-3 pb-3 ps-4 pe-4 time-<?php echo (int) str_replace(":00", "", $start_hour); ?>" data-hour="<?php echo (int) str_replace(":00", "", $start_hour); ?>">
    	<div class="podcastWrap">
          	<a href="<?php echo $program_old_slug; ?>" class="podcastBody d-flex">
            	<div class="podcast1col podcastHour">
            		<span class="h2"><?php echo $start_hour; ?></span>
            	<div class="podcast2col ms-2 me-2 ms-md-3 me-md-3">
        			<div class="playBlock">
    					<button class="playSolid btnNoBorder invertToWhite"></button>
    	        	<?php if($schedule_img): ?>
    	        		<img srcset="<?php echo $schedule_img['sizes']['mecenas90']; ?>,
    				     <?php echo $schedule_img['sizes']['redaktorSmall']; ?> 2x"
    				     src="<?php echo $schedule_img['sizes']['mecenas90']; ?>"
    				     width="90" height="90" alt="<?php echo $author_names; ?>">
    			     <?php endif; ?>
    			<div class="podcast3col">
    	        	<h3 class="podcastSubTitle mb-0 h3Inter"><?php echo $schedule_title; ?></h3>
    	        	<?php if($author_names): ?>
    	        		<div class="podcastAuthor mt-2 mb-2"><?php echo $author_names; ?></div>
    	        	<?php endif; ?>
    	    		<?php if($schedule_desc): ?>
    	        		<div class="podcastDescWrap">
    			      		<div class="podcastExpander">
    		      				<button class="plusSign btnNoBorder"></button>
    		      				<button class="minusSign btnNoBorder"></button>
    		        		<div class="podcastDesc">
    		        			<?php echo $schedule_desc; ?>
    	        	<?php endif; ?>
    				<?php include( locate_template( 'template-parts/ad-in-schedule-include.php', false, false ) ); ?>

    another include

    <?php //ACF
    	$requiredDate = date("Y-m-d");
    	/* Start the Loop */
    	$meta_query_loop = array(
    		'relation' => 'AND',
    		'program_id' => array(
    			'key' => 'program_list',
    			'value'     => $program_id,
    			'compare'   => 'LIKE'
    		'date_start' => array(
    	        'key' => 'placment_start_date',
    	        'type' => 'DATE',
    	        'value'   => $requiredDate,
    			'compare'	=> '<=',
    	    'date_end' => array(
    	        'key' => 'placment_end_date',
    	        'type' => 'DATE',
    	        'value'   => $requiredDate,
    			'compare'	=> '>=',
    	$argsLoop = array(
    		'post_type' => 'placement',
    		'post_status'    => 'publish',
    		'order'        => 'DESC',
    		'posts_per_page' => -1,
    		'meta_query' => $meta_query_loop,
    		'orderby' => array(
    			'program_id' => 'DESC',
    			'date_start' => 'DESC',
    			'date_end' => 'DESC',
    	$adloop = new WP_Query($argsLoop);
    <?php if($adloop->have_posts()): ?>
    	<div class="podcastSponsor d-flex mt-4">
    		<span class="align-self-center pt-1"><?php echo _e( 'Partnerem odcinka jest', 'customTpl' ); ?></span>
    		<div class="adPatronInner d-flex">
    			<?php while($adloop->have_posts()): $adloop->the_post(); 
    					$program_list = get_field('program_list');
    					$placment_img = get_field('placment_img', get_the_ID());
    					$placment_name = get_field('placment_name', get_the_ID());
    				<div class="adPatronInner ms-2 me-2 mb-2">
    					<img srcset="<?php echo $placment_img['sizes']['placmentBig']; ?>,
    				    	<?php echo $placment_img['sizes']['placmentBig']; ?> 2x"
    				     src="<?php echo $placment_img['sizes']['placmentBig']; ?>"
    				     width="<?php echo $placment_img['sizes']['placmentBig-width']; ?>" height="<?php echo $placment_img['sizes']['placmentBig-height']; ?>" alt="<?php echo $placment_name; ?>">
    			<?php endwhile; ?>
    <?php wp_reset_postdata(); endif; ?>
  • Your problem is that you have multiple nested queries.

    • The main WP Query
    • $scheduleloop
    • $adloop

    when you use wp_reset_postdata() at the end of the $adloop loop WP is resetting to the post in the main query, not the post in the $scheduleloop query. This function always resets to the main query.

    You can try $scheduleloop->reset_postdata()

    But I do not think this will work because $scheduleloop will be undefined in the second include.

    Even if defined you will have an issue if that template is included from multiple places so you wouldn’t know what query you need to reset. My suggestion here would be to use get_template_part() instead of include(locate_template(...)) and pass the query to be reset as an argument.

  • $scheduleloop->reset_postdata() worked. It won’t be included from any other place so this will do. Thank you very much!

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

You must be logged in to reply to this topic.