0

I have two loops on my index.php page, the first loop returns just two sticky posts for a featured content area.

The second custom loop get the other posts but excludes sticky posts, this is the loop;

  <?php
$the_query = new WP_Query( array( 
'post__not_in' => get_option( 'sticky_posts' ),


) );

if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post();
?>

            <?php get_template_part( 'content' ); ?>

<?php endwhile; endif; ?>

I believe it is this loop that is breaking the pagination and making the same posts appear on every page. If I replace this loops with just a normal loop;

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

            <?php get_template_part( 'content', get_post_format() ); ?>

<?php endwhile; endif; ?>   

Then the pagination works as it should.

This is the pagination function I have in functions.php

// Archive navigation function
function gwad_archive_navigation() {

    global $wp_query;

    if ( $wp_query->max_num_pages > 1 ) : ?>

        <ul class="archive-nav">

            <?php 
                if ( get_previous_posts_link() ) echo '<li class="archive-nav-newer">' . get_previous_posts_link( '&larr; ' . __('Previous', 'gwad')) . '</li>';
                $paged = get_query_var( 'paged' ) ? absint( get_query_var( 'paged' ) ) : 1;
                $max   = intval( $wp_query->max_num_pages );

                /** Add current page to the array */
                if ( $paged >= 1 )
                    $links[] = $paged;

                /** Add the pages around the current page to the array */
                if ( $paged >= 3 ) {
                    $links[] = $paged - 1;
                    $links[] = $paged - 2;
                }

                if ( ( $paged + 2 ) <= $max ) {
                    $links[] = $paged + 2;
                    $links[] = $paged + 1;
                }

                /** Link to first page, plus ellipses if necessary */
                if ( ! in_array( 1, $links ) ) {
                    $class = 1 == $paged ? ' active' : '';

                    printf( '<li class="number%s"><a href="%s">%s</a></li>' . "\n", $class, esc_url( get_pagenum_link( 1 ) ), '1' );

                    if ( ! in_array( 2, $links ) )
                        echo '<li>...</li>';
                }

                /** Link to current page, plus 2 pages in either direction if necessary */
                sort( $links );
                foreach ( (array) $links as $link ) {
                    $class = $paged == $link ? ' active' : '';
                    printf( '<li class="number%s"><a href="%s">%s</a></li>' . "\n", $class, esc_url( get_pagenum_link( $link ) ), $link );
                }

                /** Link to last page, plus ellipses if necessary */
                if ( ! in_array( $max, $links ) ) {
                    if ( ! in_array( $max - 1, $links ) )
                        echo '<li class="number">...</li>' . "\n";

                    $class = $paged == $max ? ' active' : '';
                    printf( '<li class="number%s"><a href="%s">%s</a></li>' . "\n", $class, esc_url( get_pagenum_link( $max ) ), $max );
                }

                if ( get_next_posts_link() ) echo '<li class="archive-nav-older">' . get_next_posts_link( __('Next', 'gwad') . ' &rarr;') . '</li>'; 
            ?>



        </ul> <!-- /archive-nav -->

    <?php endif;
}

Because I am a noob at PHP I have absolutely no idea what is going on.

The pagination does work on archive pages, page1,2,3, etc will show different posts, it is just pagination on index.php which appears to be broken.

1

I seem to have fixed the pagination issue with the custom loops, not entirely sure how it works, but it does seem to work. I have changed the custom loop to the following;

<?php
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$the_query = new WP_Query( array( 
'post__not_in' => get_option( 'sticky_posts' ),
'posts_per_page' => 6,
'paged' => $paged


) );

if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post();
?>

            <?php get_template_part( 'content' ); ?>

<?php endwhile; endif; ?>
  • The difference between this and your original query is that you are requesting a specific page number equal to the page number passed in the URL, otherwise you were always asking for the first page of results regardless of what page you were on. While this works, a much better solution is to exclude sticky posts from the main query, like in this answer. This will eliminate pagination issues automatically and won't waste resources running extra queries. – Milo Jun 18 '17 at 15:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.