Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress

cathymacars
seal-mask
.a{fill-rule:evenodd;}techdegree
cathymacars
Full Stack JavaScript Techdegree Student 15,941 Points

Better way to do multiple loops (because of problem with pagination)?

So, I need to have 3 loops on the home (blog) of this website I'm building, thus I used WP_Query (custom queries). I am having trouble with the pagination (which needs to happen for the 3rd loop). I want a << 1 | 2 | 3 | ... | 7 | 8 | 9 >> type of pagination. I've looked everywhere and tried at least 5 techniques, and the closest I've gotten was this:

<?php
    $query1 = new WP_Query( array( 'posts_per_page' => 1 ));
    if ( $query1->have_posts() ) : while ( $query1->have_posts() ) : $query1->the_post();
        // stuff...
    endwhile; wp_reset_query(); wp_reset_postdata(); endif;
?>



<?php
    $query2 = new WP_Query( array(
        'meta_key' => 'post_views_count',
        'orderby' => 'meta_value_num',
        'posts_per_page' => 3
    ));
    if ( $query2->have_posts() ) : while ( $query2->have_posts() ) : $query2->the_post();
        // stuff...
    endwhile; wp_reset_query(); wp_reset_postdata(); endif;
?>



<?php
    $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
    $query3 = new WP_Query( array(
        'posts_per_page' => 3,
        'offset' => 1,
        'paged' => $paged
    ));
    if ( $query3->have_posts() ) : while ( $query3->have_posts() ) : $query3->the_post();
        // stuff...
    endwhile;
        next_posts_link( 'Older posts', $query3->max_num_pages );
        previous_posts_link( 'Newer posts', $query3->max_num_pages );
    endif;
?>

This results in "Older" & "Newer" links (which don't seem to be working properly), but the numbers aren't there... So, if no one here can help me get this kind of pagination to work with my loops, I am open to suggestions of how to achieve the same result using any other technique for multiple/custom loops.

Help?

1 Answer

cathymacars
seal-mask
.a{fill-rule:evenodd;}techdegree
cathymacars
Full Stack JavaScript Techdegree Student 15,941 Points

@Chris Ramacciotti I've tried it and the pagination appears, but it doesn't seem to work... Kinda, I mean, the url changes from blog/ to blog/page/2/ but the same 3 posts are shown in the loop. (I've added 13 filler posts, so it's not for lack of posts). This is how I've tried to implement your suggestion; on the last loop, I've added it like this (replacing what was already there):

endwhile;
    the_posts_pagination();
endif;

What am I missing?