• Resolved [email protected]

    (@dylanpetrohilosgmailcom)


    Hey everyone, so I’m trying to reduce the number of queries on my home page by running several loops in the same query. For whatever reason, each new loop is running the initial post…

    So loop 1, 2, 3, and 4 are each displaying post 0 instead of 0, 1, 2, 3, 4 and so on…

    here’s my set up, is my theory and set up wrong? or am I missing something rather important?

    <?php 
    $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
    $args = array( 'posts_per_page' => 1, 'paged' => $paged, );
    $featured = new WP_Query($args);
    if ($featured->have_posts()) : while ($featured->have_posts()) : $featured->the_post(); 
    $do_not_duplicate = $post->ID; ?>	?>
    <?php get_template_part('front/top'); ?>
    <?php endwhile; ?>
    <?php endif; ?>
    <?php rewind_posts(); ?>
    
    <?php get_template_part('front/top-stories'); ?>    
    <div class="container"><section class="row front">
    <section class="col-lg-9 col-md-12 col-sm-12 featured-post">
        
    <?php if ($featured->have_posts()) : while ($featured->have_posts()) : $featured->the_post(); ?>
    <?php get_template_part('front/featured-loop-1'); ?>    
    <?php endwhile; ?>
    <?php endif; ?>
    <?php rewind_posts(); ?>
    
    <?php if ($featured->have_posts()) : while ($featured->have_posts()) : $featured->the_post(); ?>
    <?php get_template_part('front/featured-loop-2'); ?>    
    <?php endwhile; ?>
    <?php endif; ?>
    <?php rewind_posts(); ?>
    
    <?php if ($featured->have_posts()) : while ($featured->have_posts()) : $featured->the_post(); ?>
    <?php get_template_part('front/featured-loop-1'); ?>    
    <?php endwhile; ?>
    <?php endif; ?>
    <?php rewind_posts(); ?>
        
    </section>
        
    <?php get_template_part('front/featured-loop-3'); ?>    
    
       
        
        </section></div>
            

    here are my actual loops, loop 1:

    <?php $backgroundImg = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'full' ); ?>
    
    <div id="post-<?php the_ID(); ?>" <?php post_class('container-fluid black-bg'); ?> style="background-image: url('<?php echo $backgroundImg[0]; ?>'); background-repeat: no-repeat; background-position: center 65%;   background-blend-mode: overlay;"> 
    
        
        <section class="row">
    
    <article id="top-front-wrapper" class="col-sm-12 col-md-12 col-lg-7 col-12 top-post text-white offset-lg-1 offset-0">
    
    <h1 class="big-text">
        
        <a href="<?php the_permalink();?>"><?php the_title(); ?>
    </a>
        
        </h1>    
        
     <?php get_template_part('citation-featured'); ?>
                
    
        </article>
        
        
    <!-- No posts found -->
        </section></div>
    <div id="post-<?php the_ID(); ?>" <?php post_class('row'); ?>>
            <header class="col-lg-6 col-md-6 col-sm-12 col-12 order-lg-2 featured-content">
        <?php get_template_part('cover-front'); ?>
            </header>
            
            <article class="col-lg-6 col-md-6 col-sm-12 col-12 order-lg-1 featured-content">
    <?php get_template_part('headline'); ?>                        
    <?php get_template_part('citation'); ?>
    		<?php html5wp_excerpt('html5wp_index'); // Build your custom callback length in functions.php ?>
    
    		<?php edit_post_link(); ?>
            </article>
    
    	</div>
    <div class="col-lg-3 col-sm-12 col-md-12 double featured-post offset-lg-0 offset-0">
    
    <?php 
    $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
    $args = array( 'posts_per_page' => 3, 'paged' => $paged );
    $my_posts = new WP_Query($args);
    if ($my_posts->have_posts()) : while ($my_posts->have_posts()) : $my_posts->the_post(); $featured_side = $post->ID; ?>	
    <div id="post-<?php the_ID(); ?>" <?php post_class('featured-content row bottoms'); ?>> 
            
            <div class="col-lg-12 col-md-6 col-sm-6">
    <?php get_template_part('cover-front'); ?>
            </div>        
                
    <div class="col-lg-12 col-md-6 col-sm-6">
    <h2 class="small-text"><a href="<?php the_permalink();?>" title="<?php the_title(); ?>"><?php the_title(); ?>
        </a></h2>
            
            <?php get_template_part('citation'); ?>
    
    		<?php edit_post_link(); ?>
    
            </div>
    	</div>
    
            <?php endwhile ?>
       <?php endif ?>
    <?php wp_reset_query; ?>
    
         </div>  
    
    

    Thanks in advance! Appreciate any and all support

Viewing 6 replies - 1 through 6 (of 6 total)
  • First, your initial query says 'posts_per_page' => 1 so you are only dealing with one post.
    Second, when you call rewind_posts(), you are affecting the main query and not your featured query (that has only one post). If you look carefully at the example code on https://developer.www.ads-software.com/reference/functions/rewind_posts/ it has a custom query and uses the direct call like $featured->rewind_posts().
    Third, you are setting up your query using the main query’s paged parameter. This makes no sense. Your last code block has yet another query for 3 posts, also using the paged parameter.

    Thread Starter [email protected]

    (@dylanpetrohilosgmailcom)

    <?php 
    $args = array( 'posts_per_page' => 1);
    $featured = new WP_Query($args);
    if ($featured->have_posts()) : while ($featured->have_posts()) : $featured->the_post();?>
    <?php $do_not_duplicate = $post->ID; ?>	
    <?php get_template_part('front/top'); ?>
    <?php endwhile; endif; ?>
    <?php $featured->rewind_posts(); ?>
    
    <!---! this should be one post that looks different !---> 
    
    <?php get_template_part('front/top-stories'); ?>    
    <div class="container"><section class="row front">
    <section class="col-lg-9 col-md-12 col-sm-12 featured-post">
        
    <?php while ($featured->have_posts()) : $featured->the_post(); ?>
    <?php get_template_part('front/featured-loop-1'); ?>    
    <?php endwhile; ?>
    <?php $featured->rewind_posts(); ?>
    <!---! this should be one post that looks different !---> 
    
    <?php while ($featured->have_posts()) : $featured->the_post(); ?>
    <?php get_template_part('front/featured-loop-2'); ?>    
    <?php endwhile; ?>
    <?php $featured->rewind_posts(); ?>
    <!---! this should be one post that looks different !---> 
    
    <?php while ($featured->have_posts()) : $featured->the_post(); ?>
    <?php get_template_part('front/featured-loop-1'); ?>    
    <?php endwhile; ?>
    <?php $featured->rewind_posts(); ?>
    <!---! this should be one post that looks different !---> 
        
    </section>
       <div class="col-lg-3 col-sm-12 col-md-12 double featured-post offset-lg-0 offset-0">
           
           <section class="featured-content row bottoms">
     
    
    <?php while ($featured->have_posts()) : $featured->the_post(); ?>
    
    <?php get_template_part('front/featured-loop-3'); ?>    
    
       
    <?php endwhile; ?>
    
    <?php wp_reset_query; ?>
        
           
           
    </section></div>
        </section></div>

    @joyously appreciate the feed back and help!

    So, I’ve changed up my code, but it’s essentially doing the same thing and repeating the first post 4 times. Thanks for the help again everyone

    You still have array( 'posts_per_page' => 1) so there is only one post.

    I don’t understand what you are trying to do. Your code says “get one post, show the post as ‘front/top’, show the post as ‘front/featured-loop-1’, show the post as ‘front/featured-loop-2’, show the post as ‘front/featured-loop-1’, show the post as ‘front/featured-loop-3’, reset to main query”. That’s the first post 4 (or 5?) times.

    Even if you changed your query to get 3 posts instead of 1, your code would be calling the template parts with the same 3 posts, since that is what rewind does.

    I haven’t seen what is in your template-part files, but usually you want to keep the parts of a post together. That means having only one loop and calling the different parts in it. If you have multiple loops, you will be outputting the parts of the posts in separate places, which doesn’t sound very user friendly. Or if your template-parts output enough of the post to be useful, by using several loops, you are repeating the same thing in other parts of the page, which doesn’t sound useful.

    Thread Starter [email protected]

    (@dylanpetrohilosgmailcom)

    so after digging, I think my concept was somewhat off, appreciate the feedback!

    Basically, I ended up using this concept to kind of create another concept from here:
    https://wordpress.stackexchange.com/questions/225961/how-to-change-post-count-in-wordpress-loop

    Did you still have a question?
    That StackExchange question hints at the problem of the sticky posts, and also about how you don’t know what choice the user will make for number of posts to show.
    You can use 'sticky-posts' => false, in your featured query, but themes should not change the number of posts to show.

    Thread Starter [email protected]

    (@dylanpetrohilosgmailcom)

    I think my theory was off base, basically, I was trying to figure out how to runway fewer queries. The site I’m working on has hundreds if not thousands of posts and having a query for each style change seemed like it would eventually cause issues.

    <?php 
    $args = array( 'posts_per_page' => 7, 'category_name' => 'featured', 'paged'=>$paged);
    $featured = new WP_Query($args);
    if ($featured->have_posts()) : $count = 0; $paged = ( get_query_var('paged') > 1 ) ? get_query_var('paged') : 1 ; while ($featured->have_posts()) : $featured->the_post();  $count++;
    if ($count <= 1 && $paged === 1) : if ($count === 1);  ?>
    <?php get_template_part('front/top'); ?>
    
    <?php elseif (1 < $count && $count <= 2 && $paged === 1) :
                if ($count === 1); ?>
    <?php get_template_part('front/featured-loop-1'); ?>     
    <?php elseif (2 < $count && $count <= 3 && $paged === 1) : if ($count === 1); ?>
    <?php get_template_part('front/featured-loop-2'); ?>  
    <?php elseif (3 < $count && $count <= 4 && $paged === 1) : if ($count === 1); ?>    
    <?php get_template_part('front/featured-loop-1'); ?> 
            
    </section>
       <div class="col-lg-3 col-sm-12 col-md-12 double featured-post offset-lg-0 offset-0">
           
           <section class="featured-content row bottoms">
    
    <?php elseif (4 < $count && $count <= 8 && $paged === 1) : if ($count === 1); ?>    
    
    <?php get_template_part('front/featured-loop-3');?>  
    
    <?php else : if ($count === 9) ?>
               
    <br>
    <?php endif; endwhile; ?> <?php endif; ?>
    
    <!---! this should be a side bar of smaller older featured posts !---> 
           </section></div>          </section></div>   
    
    <?php 
    $args = array( 'posts_per_page' => 1, 'category_name' => 'podcast', 'paged'=>$paged);
    $featured = new WP_Query($args);
    if ($featured->have_posts()) : $count = 0; $paged = ( get_query_var('paged') > 1 ) ? get_query_var('paged') : 1 ; while ($featured->have_posts()) : $featured->the_post();  $count++;
    if ($count <= 1 && $paged === 1) : if ($count === 1);  ?>
    
    <?php get_template_part('front/social'); ?>    
    <?php get_template_part('front/social-loop'); ?>
    <?php endif; endwhile; ?> <?php endif; ?>
    
    <?php wp_reset_query(); ?>
    
    <?php get_template_part('front/sidebar-featured-widget'); ?>    
    

    This is what I eventually got… which is two queries instead of say 6 different ones.

    Thanks for helping me get there, appreciate it! I still need to clean up the code a bit after rethinking my idea, but that’s what I eventually created.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Single Query Running Multiple Loops, repeating posts’ is closed to new replies.