Support

Account

Home Forums Add-ons Repeater Field Repeater field with IF/ELSE based on sub_field value

Solving

Repeater field with IF/ELSE based on sub_field value

  • Goal: To show CPT1, CPT2, CPT3, and BLOG posts, with uniquely styled templates, on one page. Allow the admin to re-order the post types and select how many of each show (4,8 or 12)

    I have a repeater field, “Row Order and Qty” that allows the admin to rearrange the order and select the qty (via the second sub_field) of what is displayed. Right now I’m using two select fields – one to select one of the 4 post types and another for the qty to show per row. This also allows the admin to re-arrange the rows and change the order of the post types on the front-end.

    BUT, my WP query’s will not run in the repeater! I’ve tried resetting post data, storing the query temporarily (like we do with mult queries and pagination).. nothing works.

    The queries work fine outside of the repeater.

    Inside the repeater I am checking the value of the ‘content_type’ sub_field … then looping through an if/else group to display the correct query / markup for each type of post type.

    Here is my code::

    
    	<?php if(have_rows('row_order_and_qty')): ?>
    
    		<?php while(have_rows('row_order_and_qty')): the_row(); ?>
    
    			<?php $type = get_sub_field('content_type');?>
    
    			<?php if ( $type == 'auction'): ?>
    
    				<?php $recent_auctions = new WP_Query( array(
    						'post_type' 			=> 'auction',
    						'posts_per_page' 	=> get_sub_field('items_per_row'),
    						'orderby' 				=> 'date',
    						'order' 					=> 'DESC',
    						'post_status'     => 'publish',
    					));
    
    					//echo $query_sql = $recent_auctions->request; // check the query
    
    				if( $recent_auctions->have_posts() ): ?>
    				<h2 class="section-title mt-5"><i class="fas fa-gavel"></i> Auctions <a href="/auctions" class="font-weight-normal text-right badge badge-primary">View All</a></h2>
    				<div class="row row-cols-1 row-cols-sm-1 row-cols-md-2 row-cols-lg-4 row-cols-xl-4">
    					<?php while( $recent_auctions->have_posts() ): $recent_auctions->the_post(); ?>
    						<div class="col mb-4">
    							<?php get_template_part( 'template-parts/card', 'auction' ); ?>
    						</div>
    					<?php endwhile; ?>
    				</div><!-- row -->
    				<?php endif; //$auctions->have_posts() ?>
    
    			<?php elseif ($type == 'liquidation'): ?>
    
    			<?php $recent_liquidations = new WP_Query( array(
    						'post_type' 			=> 'liquidation',
    						'posts_per_page' 	=> get_sub_field('items_per_row'),
    						'orderby' 				=> 'date',
    						'order' 					=> 'DESC',
    						'post_status'     => 'publish',
    					));
    
    					// echo $query_sql = $recent_liquidations->request; // check the query
    
    				if( $recent_liquidations->have_posts() ): ?>
    					<h2 class="section-title mt-5"><i class="fas fa-file-invoice-dollar"></i> Liquidations <a href="/past-auctions" class="font-weight-normal text-right badge badge-primary">View All</a></h2>
    					<div class="row row-cols-1 row-cols-sm-1 row-cols-md-2 row-cols-lg-4 row-cols-xl-4">
    						<?php while( $recent_liquidations->have_posts() ): $recent_liquidations->the_post(); ?>
    							<div class="col mb-4">
    								<?php get_template_part( 'template-parts/card', 'liquidation' ); ?>
    							</div>
    						<?php endwhile; ?>
    					</div><!-- row -->
    				<?php endif; ?>
    
    			<?php elseif ($type == 'inventory'): ?>
    
    				<?php $recent_inventory = new WP_Query( array(
    					'post_type' 			=> 'inventory',
    					'posts_per_page' 	=> get_sub_field('items_per_row'),
    					'orderby' 				=> 'date',
    					'order' 					=> 'DESC',
    					'post_status'     => 'publish',
    				));
    
    				 //echo $query_sql = $recent_inventory->request; // check the query
    
    				if( $recent_inventory->have_posts() ): ?>
    					<h2 class="section-title mt-5"><i class="fas fa-award"></i> Featured Inventory <a href="/inventory" class="font-weight-normal text-right badge badge-primary">View All</a></h2>
    					<div class="row row-cols-1 row-cols-sm-1 row-cols-md-2 row-cols-lg-4 row-cols-xl-4">
    						<?php while( $recent_inventory->have_posts() ): $recent_inventory->the_post(); ?>
    							<div class="col mb-4">
    									<?php get_template_part( 'template-parts/loop', 'inventory-cards' ); ?>
    							</div>
    						<?php endwhile; ?>
    					</div><!-- row -->
    				<?php endif; ?>
    
    			<?php elseif ($type == 'studies'): ?>
    
    				<?php $recent_studies = new WP_Query( array(
    						'post_type' 			=> 'post',
    						'cat'   					=> 304 ,
    						'posts_per_page' 	=> get_sub_field('items_per_row'),
    						'orderby' 				=> 'date',
    						'order' 					=> 'DESC',
    						'post_status'     => 'publish',
    					));
    
    				 	//echo $query_sql = $recent_studies->request; // check the query
    
    					if( $recent_studies->have_posts() ): ?>
    
    						<h2 class="section-title mt-5"><i class="fas fa-file-invoice"></i> Case Studies <a href="/past-auctions" class="font-weight-normal text-right badge badge-primary">View All</a></h2>
    						<div class="row row-cols-1 row-cols-md-2 row-cols-lg-3">
    
    							<?php while( $recent_studies->have_posts() ): $recent_studies->the_post(); ?>
    								<div class="col mb-4">
    							    <div class="card h-100">
    										<div class="card-header"><a href="<?php the_permalink(); ?>"><?php echo truncate(get_the_title(), 75); ?><a/></div>
    
    										<?php if ( ( function_exists( 'has_post_thumbnail' ) ) && ( has_post_thumbnail() ) ) :  ?>
    											<a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('medium',['class' => 'card-img-top']); ?></a>
    										<?php else: ?>
    											<a href="<?php the_permalink(); ?>"><img src="<?php echo get_theme_file_uri().'/img/placeholder.png'; ?>" class="card-img-top d-block w-100" title="<?php the_title(); ?>" /></a>
    										<?php endif; ?>
    
    							      <div class="card-body d-flex flex-column">
    							        <div class="card-text mt-auto">
    
    												<?php if (have_rows('flexible_content_types')): ?>
    													<?php while (have_rows('flexible_content_types')): the_row(); ?>
    														<?php if (get_row_layout() == 'paragraph_content'): ?>
    															<?php // get the FIRST paragraph content only, truncate it ?>
    															<p class="mb-3"><?php echo truncate(get_sub_field('content'), 180); ?></p>
    															<?php break; ?>
    														<?php endif; ?>
    													<?php endwhile; ?>
    												<?php  endif; ?>
    
    												<a href="<?php the_permalink(); ?>" class="btn btn-primary btn-block mt-auto">View Case Study</a>
    
    											</div><!-- card-text -->
    							      </div><!-- card-body -->
    							    </div><!-- card -->
    								</div><!-- col -->
    							<?php endwhile; ?>
    
    					</div><!-- row -->
    
    				<?php endif; // if( $studies->have_posts() ): ?>
    
    			<?php endif; // elseif (get_sub_field('content_type') == 'studies'): ?>
    
    		<?php endwhile; // while(have_rows('row_order_and_qty')): the_row(); ?>
    
    	<?php endif; // if(have_rows('row_order_and_qty')): ?>
    
    	<?php if(get_field('latest_page_seo_cta_title', 'options')): ?>
    		<div class="content-cta cta-width-inline text-center">
    			<h2><?php the_field('latest_page_seo_cta_title', 'options') ?></h2>
    			<?php if(get_field('latest_page_seo_cta_link', 'options')): ?>
    				<div class="row">
    					<div class="col-12 col-lg justify-content-center">
    						<a href="<?php the_field('latest_page_seo_cta_link', 'options') ?>" class="btn btn-success"><?php the_field('latest_page_seo_cta_link_text', 'options') ?></a>
    					</div>
    				</div><!-- end div.row -->
    			<?php endif; ?>
    		</div><!-- end div.content-cta -->
    	<?php endif; ?>
    
    

    If I just use the repeater and fields, like so:

    	<?php if(have_rows('row_order_and_qty')): ?>
    
    		<?php while(have_rows('row_order_and_qty')): the_row(); ?>
    
    			<?php if (get_sub_field('content_type') == 'auction'): ?>
    				<?php the_sub_field('content_type'); ?>
    				<?php the_sub_field('items_per_row'); //echo $wp_query->request;?>
    			<?php elseif (get_sub_field('content_type') == 'liquidation'): ?>
    				<?php the_sub_field('content_type'); ?>
    				<?php the_sub_field('items_per_row'); //echo $wp_query->request;?>
    			<?php elseif (get_sub_field('content_type') == 'inventory'): ?>
    				<?php the_sub_field('content_type'); ?>
    				<?php the_sub_field('items_per_row'); //echo $wp_query->request;?>
    			<?php elseif (get_sub_field('content_type') == 'studies'): ?>
    				<?php the_sub_field('content_type'); ?>
    				<?php the_sub_field('items_per_row'); //echo $wp_query->request;?>
    			<?php endif; // elseif ($type == 'studies'): ?>
    
    		<?php endwhile; ?>
    
    	<?php endif; ?>
    

    It works!

    If I just run the queries, like so:

    
    				<?php $recent_auctions = new WP_Query( array(
    						'post_type' 			=> 'auction',
    						'posts_per_page' 	=> get_sub_field('items_per_row'),
    						'orderby' 				=> 'date',
    						'order' 					=> 'DESC',
    						'post_status'     => 'publish',
    					));
    
    					//echo $query_sql = $recent_auctions->request; // check the query
    
    				if( $recent_auctions->have_posts() ): ?>
    				<h2 class="section-title mt-5"><i class="fas fa-gavel"></i> Auctions <a href="/auctions" class="font-weight-normal text-right badge badge-primary">View All</a></h2>
    				<div class="row row-cols-1 row-cols-sm-1 row-cols-md-2 row-cols-lg-4 row-cols-xl-4">
    					<?php while( $recent_auctions->have_posts() ): $recent_auctions->the_post(); ?>
    						<div class="col mb-4">
    							<?php get_template_part( 'template-parts/card', 'auction' ); ?>
    						</div>
    					<?php endwhile; ?>
    				</div><!-- row -->
    				<?php endif; //$auctions->have_posts() ?>
    			<?php $recent_liquidations = new WP_Query( array(
    						'post_type' 			=> 'liquidation',
    						'posts_per_page' 	=> get_sub_field('items_per_row'),
    						'orderby' 				=> 'date',
    						'order' 					=> 'DESC',
    						'post_status'     => 'publish',
    					));
    
    					// echo $query_sql = $recent_liquidations->request; // check the query
    
    				if( $recent_liquidations->have_posts() ): ?>
    					<h2 class="section-title mt-5"><i class="fas fa-file-invoice-dollar"></i> Liquidations <a href="/past-auctions" class="font-weight-normal text-right badge badge-primary">View All</a></h2>
    					<div class="row row-cols-1 row-cols-sm-1 row-cols-md-2 row-cols-lg-4 row-cols-xl-4">
    						<?php while( $recent_liquidations->have_posts() ): $recent_liquidations->the_post(); ?>
    							<div class="col mb-4">
    								<?php get_template_part( 'template-parts/card', 'liquidation' ); ?>
    							</div>
    						<?php endwhile; ?>
    					</div><!-- row -->
    				<?php endif; ?>
    				<?php $recent_inventory = new WP_Query( array(
    					'post_type' 			=> 'inventory',
    					'posts_per_page' 	=> get_sub_field('items_per_row'),
    					'orderby' 				=> 'date',
    					'order' 					=> 'DESC',
    					'post_status'     => 'publish',
    				));
    
    				 //echo $query_sql = $recent_inventory->request; // check the query
    
    				if( $recent_inventory->have_posts() ): ?>
    					<h2 class="section-title mt-5"><i class="fas fa-award"></i> Featured Inventory <a href="/inventory" class="font-weight-normal text-right badge badge-primary">View All</a></h2>
    					<div class="row row-cols-1 row-cols-sm-1 row-cols-md-2 row-cols-lg-4 row-cols-xl-4">
    						<?php while( $recent_inventory->have_posts() ): $recent_inventory->the_post(); ?>
    							<div class="col mb-4">
    									<?php get_template_part( 'template-parts/loop', 'inventory-cards' ); ?>
    							</div>
    						<?php endwhile; ?>
    					</div><!-- row -->
    				<?php endif; ?>
    				<?php $recent_studies = new WP_Query( array(
    						'post_type' 			=> 'post',
    						'cat'   					=> 304 ,
    						'posts_per_page' 	=> get_sub_field('items_per_row'),
    						'orderby' 				=> 'date',
    						'order' 					=> 'DESC',
    						'post_status'     => 'publish',
    					));
    
    				 	//echo $query_sql = $recent_studies->request; // check the query
    
    					if( $recent_studies->have_posts() ): ?>
    
    						<h2 class="section-title mt-5"><i class="fas fa-file-invoice"></i> Case Studies <a href="/past-auctions" class="font-weight-normal text-right badge badge-primary">View All</a></h2>
    					<div class="row row-cols-1 row-cols-sm-1 row-cols-md-2 row-cols-lg-4 row-cols-xl-4">
    
    							<?php while( $recent_studies->have_posts() ): $recent_studies->the_post(); ?>
    								<div class="col mb-4">
    							    <div class="card h-100">
    										<div class="card-header"><a href="<?php the_permalink(); ?>"><?php echo truncate(get_the_title(), 75); ?><a/></div>
    
    										<?php if ( ( function_exists( 'has_post_thumbnail' ) ) && ( has_post_thumbnail() ) ) :  ?>
    											<a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('medium',['class' => 'card-img-top']); ?></a>
    										<?php else: ?>
    											<a href="<?php the_permalink(); ?>"><img src="<?php echo get_theme_file_uri().'/img/placeholder.png'; ?>" class="card-img-top d-block w-100" title="<?php the_title(); ?>" /></a>
    										<?php endif; ?>
    
    							      <div class="card-body d-flex flex-column">
    							        <div class="card-text mt-auto">
    
    												<?php if (have_rows('flexible_content_types')): ?>
    													<?php while (have_rows('flexible_content_types')): the_row(); ?>
    														<?php if (get_row_layout() == 'paragraph_content'): ?>
    															<?php // get the FIRST paragraph content only, truncate it ?>
    															<p class="mb-3"><?php echo truncate(get_sub_field('content'), 180); ?></p>
    															<?php break; ?>
    														<?php endif; ?>
    													<?php endwhile; ?>
    												<?php  endif; ?>
    
    												<a href="<?php the_permalink(); ?>" class="btn btn-primary btn-block mt-auto">View Case Study</a>
    
    											</div><!-- card-text -->
    							      </div><!-- card-body -->
    							    </div><!-- card -->
    								</div><!-- col -->
    							<?php endwhile; ?>
    
    					</div><!-- row -->
    
    				<?php endif; // if( $studies->have_posts() ): ?>
    
    

    It works..

    #facepalm… see anything that I’m doing wrong or that needs adjusting???

  • Clarification… if i run the queries outside the if repeater, I get ALL the correct post types showing.

    IF I run it IN the repeater, only the first one shows.

    I’ve also tested this with an switch statement,.. and only get one of the post types… not all of them.

  • In the admin,
    I have these four rows of fields (post type / qty per row):

    inventory 4
    liquidation 4
    auction 8
    studies 8

    As a test, if I run this code:

    <?php switch ($type):
    	case 'auction': ?>
    		<?php the_sub_field('content_type'); ?>
    		<?php the_sub_field('items_per_row'); //echo $wp_query->request;?>
    	<?php break; case 'liquidation': ?>
    		<?php the_sub_field('content_type'); ?>
    		<?php the_sub_field('items_per_row'); //echo $wp_query->request;?>
    	<?php break; case 'inventory': ?>
    		<?php the_sub_field('content_type'); ?>
    		<?php the_sub_field('items_per_row'); //echo $wp_query->request;?>
    	<?php break; case 'studies': ?>
    		<?php the_sub_field('content_type'); ?>
    		<?php the_sub_field('items_per_row'); //echo $wp_query->request;?>
    	<?php break; default: ?>
    		<?php echo 'No posts to show'; ?>
    <?php break; endswitch; ?>
    

    I get:

    inventory 4 liquidation 4 auction 8 studies 8
    

    Which is correct. It’s when I replace the sub_fields in those switch cases, or the if/else statements that the loop stops on the first one.. and I get:

    inventory 4
    
  • I don’t see anything in the code that you’ve posted that might cause this.

    Could it be something in one of the template parts your calling for each post?

  • I’ve checked the template files, one has a loop for a flexible content field. So, for case studies (basic blog post/category, not CPT) I have flexible content fields. I loop through just one of them (in the template include) and break after the first ‘paragraph’ content field… just to get the content for that case study.

    If I remove that flexible content loop from the include.. the page LOADS but with only the first row of the repeater! IF I use get_posts instead of WP query, it loads all of the rows…

    Here is the flexible content loop from the case-studies.php file:

    			<?php if (have_rows('flexible_content_types')): ?>
    				<?php while (have_rows('flexible_content_types')): the_row(); ?>
    					<?php if (get_row_layout() == 'paragraph_content'): ?>
    						<?php // get the FIRST paragraph content only, truncate it ?>
    						<p class="mb-3"><?php echo truncate(get_sub_field('content'), 180); ?></p>
    						<?php break; ?>
    					<?php endif; ?>
    				<?php endwhile; ?>
    			<?php  endif; ?>
    
  • I am lost at what is happening and what is only appearing once.

    Is it the rows of the initial repeater?

    
    while(have_rows('row_order_and_qty'))
    

    Only one row is showing?

  • Yes. only a single row of the initial repeater will show, if I REMOVE the flex field from the case-studies template file.

    With the Flex field in place, the page spools till it times out.

    If I remove the flex field and use get_posts instead of wp query, all of them show.. !

  • By the sound of it, somehow there is something missing, or for some reason an ‘endif’ or ‘endwhile’ is not being executed. It’s hard form me to tell, the code you’ve posted is hard for me to read and with the syntax you used it’s not possible for my IDE to figure out what is not matched correctly.

    When the flex field is included there is an END SOMETHING that is being executed that stops the loop over the repeater causing only one row to be shown. When the flex field is not include there is and END SOMETHING that is not being executed causing an infinite loop in whatever is being output.

  • Thank you for the time to look over the code.

    My initial goal was to show all the CPTs and Posts, on one page, sorted by date.

    But, I’ve never updated $args within a loop… so, heading this direction of showing rows of each type.

    My original logic for this was:

        $my_post_types = array( 'auctions', 'liquidations', 'inventory',  'post' );
    
        // set args for ALL posts
        $args = array(
            'post_type'     => $my_post_types,
            'orderby'       => 'date',
            'order'         => 'DESC',
            'post_status'   => 'publish',
        );
        
        Now we loop through posts (while loop)
    
        IF 'post_type == auctions' 
            change number_posts to $args from get_sub_field('items_per_row')
            get_template_part( 'template-parts/card', 'auction' );  
    
        IF 'post_type == liquidations' 
            change number_posts to $args from get_sub_field('items_per_row')
            get_template_part( 'template-parts/card', 'liquidations' );  
    
        IF 'post_type == inventory' 
            change number_posts to $args from get_sub_field('items_per_row')
            get_template_part( 'template-parts/card', 'inventory' );  
    
        IF 'post_type == post' 
            add 'cat => 304' to $args 
            change number_posts to $args from get_sub_field('items_per_row')
            and get_template_part( 'template-parts/card', 'studies' );  
    

    Since it was to be sorted by date, I wouldn’t need to setup the fields like I have now (so they can re-order them). I just need to allow them to choose the qty of posts shown for each type, let the loop sort by date.

    Any thoughts on this approach?

  • The first thing I would do is simplify the code. You don’t need all those if/elseif statements and you can reduce the number of post loops to 1 using the values from the sub fields.

    But you will need to build a card template part for the case studies

    
    <?php 
      
      if (have_rows('row_order_and_qty')) {
        while(have_rows('row_order_and_qty')) {
          the_row();
          $post_type = get_sub_field('content_type');
          $posts_per_page = get_sub_field('items_per_row');
          $row_query = new WP_Query(array(
            'post_type' => $post_type,
            'post_status' => 'publish',
            'posts_per_page' => $posts_per_page,
            'orderby' => 'date',
            'order' => 'DESC'
          ));
          if ($row_query->have_posts()) {
            switch($post_type) {
              case 'auction':
                $icon = '<i class="fas fa-gavel"></i>';
                $heading = 'Auctions';
                $class = 'row row-cols-1 row-cols-sm-1 row-cols-md-2 row-cols-lg-4 row-cols-xl-4';
                $link_url = '/auctions';
                break;
              case 'liquidation':
                $icon = '<i class="fas fa-file-invoice-dollar"></i>';
                $heading = 'Liquidations';
                $class = 'row row-cols-1 row-cols-sm-1 row-cols-md-2 row-cols-lg-4 row-cols-xl-4';
                $link_url = '/past-auctions';
                break;
              case 'inventory':
                $icon = '<i class="fas fa-award">';
                $heading = 'Featured Inventory';
                $class = 'row row-cols-1 row-cols-sm-1 row-cols-md-2 row-cols-lg-4 row-cols-xl-4';
                $link_url = '/inventory';
                break;
              case 'studies':
                $icon = '<i class="fas fa-file-invoice"></i>';
                $heading = 'Case Studies';
                $class = 'row row-cols-1 row-cols-md-2 row-cols-lg-3';
                $link_url = '/studies';
                break;
            } // end switch
            ?>
              <h2 class="section-title mt-5"><?php 
                echo $icon; ?> <?php 
                echo $heading; ?> 
                <a href="<?php 
                  echo $link_url; ?>" class="font-weight-normal text-right badge badge-primary">View All</a></h2>
              <div class="<?php echo $class; ?>">
                <?php 
                  while ($row_query->have_posts()) {
                    $row_query->the_post();
                    ?>
                      <div class="col mb-4">
                        <?php get_template_part('template-parts/card', $type); ?>
                      </div>
                    <?php 
                  } // end while have posts
                  wp_reset_postdata();
                ?>
              </div>
            <?php 
          } // end if have posts
        } // end while have rows
      } // end if have rows
    ?>
    
  • I tried your code, very clean by the way! The $type var needed to be updated to $post_type… but other than that the vars in the switch case come back as undefined

    Notice: Undefined variable: icon, etc…

    Thoughts?? It’s like the switch case is having no effect on setting those vars

  • if you change the switch to switch($post_type) { it should work. Not working confuses me unless there is something I’m missing for the value of the content_type sub field

  • Yeah, I made the switch the $post_type var… but, doesn’t work.
    I even added some echos so see if I could get the values to print

          $post_type = get_sub_field('content_type'); echo $post_type;
    
          $posts_per_page = get_sub_field('items_per_row');echo $posts_per_page;
    

    But nothing displays (I also tried after removing the echos below, to get rid of the errors but no values from the ACF fields show). I also added a default value to the switch, just in case, and it doesn’t print either
    .

  • Can’t believe I missed this.. it was missing row(); after the while statement.

    But, still only prints the first post in the loop.

    <?php
    
      if (have_rows('row_order_and_qty')) {
        while(have_rows('row_order_and_qty')){ the_row();
    
          $post_type = get_sub_field('content_type'); echo $post_type;
    
          $posts_per_page = get_sub_field('items_per_row'); echo $posts_per_page;
    
          $row_query = new WP_Query(array(
            'post_type' => $post_type,
            'post_status' => 'publish',
            'posts_per_page' => $posts_per_page,
            'orderby' => 'date',
            'order' => 'DESC'
          ));
    
          if ($row_query->have_posts()) {
            switch($post_type) {
              case 'auction':
                $icon = '<i class="fas fa-gavel"></i>';
                $heading = 'Auctions';
                $class = 'row row-cols-1 row-cols-sm-1 row-cols-md-2 row-cols-lg-4 row-cols-xl-4';
                $link_url = '/auctions';
                break;
              case 'liquidation':
                $icon = '<i class="fas fa-file-invoice-dollar"></i>';
                $heading = 'Liquidations';
                $class = 'row row-cols-1 row-cols-sm-1 row-cols-md-2 row-cols-lg-4 row-cols-xl-4';
                $link_url = '/past-auctions';
                break;
              case 'inventory':
                $icon = '<i class="fas fa-award">';
                $heading = 'Featured Inventory';
                $class = 'row row-cols-1 row-cols-sm-1 row-cols-md-2 row-cols-lg-4 row-cols-xl-4';
                $link_url = '/inventory';
                break;
              case 'studies':
                $icon = '<i class="fas fa-file-invoice"></i>';
                $heading = 'Case Studies';
                $class = 'row row-cols-1 row-cols-md-2 row-cols-lg-3';
                $link_url = '/studies';
                break;
            } // end switch
            ?>
              <h2 class="section-title mt-5"><?php echo $icon; ?> <?php echo $heading; ?> <a href="<?php echo $link_url; ?>" class="font-weight-normal text-right badge badge-primary">View All</a></h2>
              <div class="<?php echo $class; ?>">
                <?php
                  while ($row_query->have_posts()) {
                    $row_query->the_post(); ?>
                      <div class="col mb-4">
                        <?php // get_template_part('template-parts/card', $type); ?>
                      </div>
                    <?php
                  } // end while have posts
                  //wp_reset_postdata();
                ?>
              </div>
            <?php
          } // end if have posts
        } // end while have rows
      } // end if have rows
    ?>
    

    Link to Results of current code

  • if these are not getting any values then there is an issue with these fields

    
    $post_type = get_sub_field('content_type');
    $posts_per_page = get_sub_field('items_per_row');
    

    are you sure these are the fields? Are they nested inside of something else like a group field or something?

    Obviously row_order_and_qty has rows or nothing would happen so it has to be something about the sub fields.

  • Does in print the first post in each group or just the first post in the first group?

  • Just prints the first period.. one showing.

    here is the code so far

    <?php
    
      if (have_rows('row_order_and_qty')) {
        while(have_rows('row_order_and_qty')){ the_row();
    
          $post_type = get_sub_field('content_type'); echo $post_type;
    
          $posts_per_page = get_sub_field('items_per_row'); echo $posts_per_page;
    
          $row_query = new WP_Query(array(
            'post_type' => $post_type,
            'post_status' => 'publish',
            'posts_per_page' => $posts_per_page,
            'orderby' => 'date',
            'order' => 'DESC'
          ));
    
          if ($row_query->have_posts()) {
            switch($post_type) {
              case 'auction':
                $icon = '<i class="fas fa-gavel"></i>';
                $heading = 'Auctions';
                $class = 'row row-cols-1 row-cols-sm-1 row-cols-md-2 row-cols-lg-4 row-cols-xl-4';
                $link_url = '/auctions';
                break;
              case 'liquidation':
                $icon = '<i class="fas fa-file-invoice-dollar"></i>';
                $heading = 'Liquidations';
                $class = 'row row-cols-1 row-cols-sm-1 row-cols-md-2 row-cols-lg-4 row-cols-xl-4';
                $link_url = '/past-auctions';
                break;
              case 'inventory':
                $icon = '<i class="fas fa-award">';
                $heading = 'Featured Inventory';
                $class = 'row row-cols-1 row-cols-sm-1 row-cols-md-2 row-cols-lg-4 row-cols-xl-4';
                $link_url = '/inventory';
                break;
              case 'studies':
                $icon = '<i class="fas fa-file-invoice"></i>';
                $heading = 'Case Studies';
                $class = 'row row-cols-1 row-cols-md-2 row-cols-lg-3';
                $link_url = '/studies';
                break;
            } // end switch
            ?>
              <h2 class="section-title mt-5"><?php echo $icon; ?> <?php echo $heading; ?> <a href="<?php echo $link_url; ?>" class="font-weight-normal text-right badge badge-primary">View All</a></h2>
              <div class="<?php echo $class; ?>">
                <?php
                  while ($row_query->have_posts()) {
                    $row_query->the_post(); ?>
                      <div class="col mb-4">
                        <?php // get_template_part('template-parts/card', $type); ?>
                      </div>
                    <?php
                  } // end while have posts
                  //wp_reset_postdata();
                ?>
              </div>
            <?php
          } // end if have posts
        } // end while have rows
      } // end if have rows
    ?>
    

    Image of results so far

  • Here are the ACF fields and Admin views:

    Admin

    Fields

  • If I just run this:

      if (have_rows('row_order_and_qty')) {
        while(have_rows('row_order_and_qty')){ the_row();
    
          $post_type = get_sub_field('content_type'); echo $post_type;
    
          $posts_per_page = get_sub_field('items_per_row'); echo $posts_per_page;
    

    to show the ACF values.. I get the correct values:

    inventory4 auction8 liquidation4 studies8

    Something happens when this code is used in a loop or something. I’ve never seen this before, it usually just works LOL

  • At this point the only think I can think of is that there is some kind of filter happening when you do a WP_Query that is somehow altering something that should not be altered.

    Do you have any pre_get_posts filters active?

  • Yes, I have some pre_get_post filters in place for the CPT pages.

    function my_pre_get_posts( $query ) {
    	global $wpdb;
    
    	// We do not want unintended consequences.
    	if ( is_admin() || !$query->is_main_query() ) {
    		return $query;
    	}
    
    	// Adding all CPTUI post types to the archives
    	if ( is_category() && empty( $query->query_vars['suppress_filters']) ) {
    		// Replace these slugs with the post types you want to include.
    		$cptui_post_types = array( 'inventory', 'auction', 'liquidation' );
    		$query->set(
    	  		'post_type',
    			array_merge(
    				array( 'post' ),
    				$cptui_post_types
    			)
    		);
    	}
    
    	// Include CPTUI post types in Search Results
    	if ( $query->is_search ) {
    
          $query->set( 'post_type', array( 'page','post', 'inventory', 'auction', 'liquidation' ) );
    
      }	elseif(	isset($query->query_vars['post_type']) && $query->query_vars['post_type'] == 'auction' ) {
    
    			$query->set('numberposts', 12);
    
    	} elseif( $query->query_vars['category_name'] == 'packaging-equipment' || $query->query_vars['category_name'] == 'processing-equipment' || isset($query->query_vars['post_type']) && $query->query_vars['post_type'] == 'inventory') {
    
    			// remove the archived items from view
    			$query->set( 'cat', '-307' );
    
    			// set the orderby so NEWEST and AVAILABLE are first.
          $query->set( 'meta_query', array(
                array( 'key' => 'statecode' )
            ));
        // another way
        // $query->set('meta_key', 'statecode');
    
    			$query->set( 'orderby', array('meta_value' => 'ASC','date' => 'DESC') );
    
    		// IF item_oem querystring is present, filter posts by value
    		if( isset($_GET['item_oem'] ) && !empty($_GET['item_oem'] )) {
    
    		  // Making the item safe for use.  So there is now HTML embedded.
    		  $oem = htmlspecialchars($_GET['item_oem'], ENT_QUOTES);
    
    			$query->set('post_type', 'inventory');
    			$query->set('meta_key', 'item_oem');
    			$query->set('meta_value', $oem);
    			$query->set('meta_compare', '=' );
    			$query->set( 'orderby', 'meta_value' );
    			$query->set( 'order', 'ASC' );
    
    		// IF show_per_page query string is set, update numberposts to param value to allow visitor to show specific number of posts per page
    	} elseif( isset( $_GET['show_per_page'] ) && !empty($_GET['show_per_page']) ) {
    
    		$show_per_page = htmlspecialchars($_GET['show_per_page'], ENT_QUOTES);
    		$query->set('posts_per_page', $show_per_page);
    
    	} elseif( isset( $_GET['newly_acquired'] ) && $_GET['newly_acquired'] == 'true' && is_post_type_archive('inventory')) {
    
    			$query->set('post_type', 'inventory');
    
    			// let's only show items from the last 30 days.. when the button is clicked.
    			$date_query = array(
     				array(
     					'after' => '-30 days',
     					'column'=> 'post_date',
     					),
    				);
    
    			// set the date for the query
    			$query->set( 'date_query', $date_query 	);
    
    		} else {
    
    			$query->set( 'posts_per_page', 12 );
    
    		}
    	}
    
    	// set loop for articles, employment and case study archives
    }
    add_filter( 'pre_get_posts', 'my_pre_get_posts', 99999);
    
  • If I comment the pre_get_posts out.. there is no change

  • did you try it without this line commented out

    
    //wp_reset_postdata();
    
  • without resetting post data when have_rows() is called in the second iteration it uses get_the_ID() to get the post ID in the current loop. If you do not reset the post data then it will get the post ID from the last post in the nested query loop. Since there is not rows for this repeater on that post it returns false.

  • THAT did it! I had tried with it NOT commented, and the page wouldn’t load.. so I commented it out.
    Now, works.. must have been the other errors that thru the wp_reset for a loop – no pun intended.

    Thank you. for all your help!

Viewing 25 posts - 1 through 25 (of 29 total)

You must be logged in to reply to this topic.