Support

Account

Home Forums Add-ons Repeater Field Query posts within taxonomy via repeater

Solving

Query posts within taxonomy via repeater

  • Yep, it’s me again. Working on querying chosen taxonomies and displaying them in a tab layout. So far my tab titles are coming through great. The problem is that I can’t seem to get the posts associated for each of those terms to display in the tab content area. This is what I’ve got for my code:

    	<!-- Nav Tabs -->
    	<ul  class="nav nav-pills">
    		<?php if (have_rows('home_categories')) {
    			$i = 0;
        		while (have_rows('home_categories')) {
    	    		the_row();
    				$term = get_sub_field('categories'); ?>
    		<li class="<?php if ($i == 0) { echo ' active'; }?>"> 
    			<a href="#<?php echo $term->name; ?>" data-toggle="tab"><?php echo $term->name; ?></a>
    		</li>
    			<?php $i++;
    			}
    		} ?>
    	</ul>
    	
    	<!-- Tab Content -->
    	<div class="tab-content clearfix">
    		<?php if (have_rows('home_categories')) {
    			$i = 0;
    			while (have_rows('home_categories')) {
    				the_row();
    				
    				$args = array(
    					'post_type' => 'post',
    					'taxonomy' => $term->name,
    					'posts_per_page' => 5,
    					'orderby' => 'date',
    					'order' => 'ASC',
    				);
    				$post = new WP_Query( $args );
    				while( $post->have_posts() ) : $post->the_post();
    				
    				$term = get_sub_field('categories'); ?>
    				
    					<div class="tab-pane fade<?php if ($i == 0) { echo ' in active'; }?>" id="<?php echo $term->name; ?>">
    						<?php the_post_thumbnail('thumbnail', array('class' => 'img-responsive img-thumbnail')); ?>
    						<h3><?php the_title(); ?></h3>
    						<?php the_excerpt(); ?>
    					</div>
    				<?php endwhile;
    			}
    			$i++;
    		}
    			wp_reset_postdata(); ?>
    	</div>
  • First thing you should do is add reset_rows() between the first repeater loop and the second.

    The second thing is that your query should look like this

    
    $args = array(
      'post_type' => 'post',
      'tax_query' => array(
        array(
          'taxonomy' => $term->taxonomy,
          'terms' => array($term_id);
        )
      ),
      'posts_per_page' => 5,
      'orderby' => 'date',
      'order' => 'ASC',
    );
    

    https://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters

  • I apologize for the delay in responding. I see the problem with the query and thank you there. However, I’m not understanding the reset_rows() item. Where, exactly, between the two loops does it go? Does it replace reset_postdata? Or does it go before or after that? I’m afraid this is one I haven’t run into before.

  • I should note that I have tried putting the reset_rows() bit right before Tab Content. It seems to be causing my page to slow to a crawl, and isn’t generating anything in that content area at all now.

  • Maybe you don’t need to add reset_rows. If could be that I’m mistaken about it’s use in this case. If you are getting output in both sections without it then ignore that.

  • Unfortunately no content is being created inside the tabs now at all, regardless of whether reset_rows() is in there. Before changing the args, I was getting content, just the *wrong* content. So I’m really confused now.

  • So I looked a bit closer at your code, and mine, there were other errors in both. I think this might do what you’re looking for.

    
    <!-- Nav Tabs -->
    <ul class="nav nav-pills">
      <?php 
        if (have_rows('home_categories')) {
          $i = 0;
          while (have_rows('home_categories')) {
            the_row();
            $term = get_sub_field('categories');
              ?>
                <li class="<?php 
                    if ($i == 0) {
                      echo 'active';
                    }
                    ?>"><a href="#<?php 
                    echo $term->name; ?>" data-toggle="tab"><?php 
                    echo $term->name; ?></a>
                </li>
              <?php
            $i++;
          }
        }
      ?>
    </ul>
    <?php 
      // if your not getting anything below try reset_rows()
      // uncomment the next line
      //reset_rows();
    ?>  
    <!-- Tab Content -->
    <div class="tab-content clearfix">
      <?php 
        if (have_rows('home_categories')) {
          $i = 0;
          while (have_rows('home_categories')) {
            the_row();
            
            // moved this line up before query that uses it
            $term = get_sub_field('categories');
          
            $args = array(
              'post_type' => 'post',
              'tax_query' => array(
                array(
                  'taxonomy' => $term->taxonomy,
                  // corrected terms to use correct value
                  'terms' => array($term->term_id)
                )
              ),
              'posts_per_page' => 5,
              'orderby' => 'date',
              'order' => 'ASC',
            );
            
            // if your still not getting anything
            // uncomment and make sure the query args look right
            //echo '<pre>'; print_r($args); echo '</pre>';
            
            // renamed query to not use global $post variable
            $query = new WP_Query($args);
            
            while  ($query->have_posts()) {
              $query->the_post();
              ?>
                <div class="tab-pane fade<?php 
                    if ($i == 0) {
                      echo ' active';
                    }
                    ?>" id="<?php echo $term->name; ?>">
                  <?php 
                    the_post_thumbnail('thumbnail', array('class' => 'img-responsive img-thumbnail'));
                  ?>
                  <h3><?php the_title(); ?></h3>
                  <?php the_excerpt(); ?>
                </div>
              <?php 
            } // end while have posts
            $i++;
          } // end while have rows
          wp_reset_postdata();
        } // end if have rows
      ?>
    </div>
    
  • Hmmmm…still did not work. HOWEVER, I decided to just re-do that whole section (the tab content area) from scratch and in doing so was able to get it to work using a different method of grabbing the posts. Instead of ‘while’, I did ‘foreach’. I’m still not sure why the other way wasn’t working. I feel like it *should* have. But here’s what I got working (I can’t take *all* the credit…my programming-genius friend did help me here, but he’s just not always available).

    	<!-- Nav Tabs -->
    	<ul  class="nav nav-pills">
    		<?php if (have_rows('home_categories')) {
    			$i = 0;
        		while (have_rows('home_categories')) {
    	    		the_row();
    				$term = get_sub_field('categories'); ?>
    		<li class="<?php if ($i == 0) { echo ' active'; }?>"> 
    			<a href="#tab-pane-<?php echo $i; ?>" data-toggle="tab"><?php echo $term->name; ?></a>
    		</li>
    			<?php $i++;
    			}
    		} ?>
    	</ul>
    	
    	<!-- Tab Content -->
    	<div class="tab-content clearfix">
    		<?php 
    			
    			$home_categories = get_field("home_categories");
    		//	print_r($home_categories);
    			
    			foreach($home_categories as $key => $home_cat) {
    				
    				
    				$term_id = $home_cat['categories']->term_id;
    				$term_name = $home_cat['categories']->name;
    				
    				?>
    				<div class="tab-pane fade<?php if ($key == 0) { echo ' in active'; }?>" id="tab-pane-<?php echo $key; ?>">
    				<?php
    				
    				
    				$args = array(
    					'post_type' => 'post',
    					'tax_query' => array(
    						array(
    							'taxonomy' => $term->taxonomy,
    							'terms' => array($term_id)
    						)
    					),
    					'posts_per_page' => 5,
    					'orderby' => 'date',
    					'order' => 'ASC',
    				);
    				$query = new WP_Query( $args );
    				
    				foreach($query->posts as $post) {
    					
    					?>
    					
    						<a href="<?php echo get_permalink($post->ID); ?>"><?php echo get_the_post_thumbnail($post->ID, 'thumbnail', array('class' => 'img-responsive img-thumbnail')); ?></a>
    						<a href="<?php echo get_permalink($post->ID); ?>"><h3><?php echo get_the_title($post->ID); ?></h3></a>
    						<?php the_excerpt(); ?>
    					<?php
    					
    					
    				}
    								
    				?>
    				</div>
    				<?php	
    					
    					
    				
    			}			?>
    
    	</div>
  • My guess has to do with reset_rows() because you have already looped over them all.

    It’s like this if you where looping over posts

    
    if (have_posts()) {
      while(have_posts()) {
        the_post();
        //  do stuff
      }
      while(have_posts()) {
        // never happens because you're already looped over them
        // you need to use rewind_posts() before this second loop
        // reset_rows() works the same way
      }
    }
    
Viewing 9 posts - 1 through 9 (of 9 total)

The topic ‘Query posts within taxonomy via repeater’ is closed to new replies.