1

I have a custom post type with it's own archive page. I'm running a custom loop, displaying 1 result. I'm trying to paginate these results, but I'm having some strange issues that I can't seem to figure out.

Here's my WP_Query:

$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$args = array(
    'post_type'         => 'news-events',
    'posts_per_page'    => 1,
    'order'             => 'desc',      
    'paged'             => $paged
);
$the_query = new WP_Query( $args );

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

Here's the code I'm using to make the forward and backward links:

if ( $the_query->max_num_pages > 1 ) : // check if the max number of pages is greater than 1  ?>
    <nav class="prev-next-posts">
        <div class="prev-posts-link">
            <?php echo get_next_posts_link( '« Previous', $the_query->max_num_pages ); // display older posts link ?>
        </div>
        <div class="next-posts-link">
            <?php echo get_previous_posts_link( 'Newer »' ); // display newer posts link ?>
        </div>
    </nav>
<?php endif; ?>

The links that are being generated seem to look OK: mysite.com/news-events/page/2/ however these 404. The strange thing is, if I manaully change this link from mysite.com/news-events/page/2/ to mysite.com/news-events/page/1/ that redirects to `mysite.com/news-events/.

I've tried re-saving my permalink structure, and changing it. I've tried disabling plugins etc, I'm really out of ideas!

Any help is appreciated, thanks.

  • 1
    I am almost 100% sure you have this issue – Pieter Goosen Mar 14 '16 at 11:13
  • @PieterGoosen If that was my issue, changing the archive page to a usual page should solve the issue, shouldn't it? I tried that, and the issue still persists. – Daniel Dewhurst Mar 14 '16 at 11:46
  • 3
    If this is a normal archive page, you should not be running custom queries in place of the main query. Remove the custom query completely, and alter the main query with pre_get_posts. – Pieter Goosen Mar 14 '16 at 11:52
1

Pieter was correct.

It was messing up due to the custom loop I was running on the archive page. I removed the custom loop, and then adjusted the arguments with pre_get_posts.

The strange thing was that, to test if it was this issue, running a custom loop on an archive page was the issue, I removed the archive page, and made a normal page with the custom loop, and I had the same issue.

I'm not sure how the above happened, I was probably just doing something else wrong.

Nevertheless, pagination is working now on my archive page, with the altered main loop.

  • I still believe you had this issue. If your posts per page in backend is set to 10, and you less than 10 posts, the main query will calculate that there will only be one page. If you try to navigate to page 2, regardless if your custom query have 10 pages, the main query will 404 and return a 404 page. That is one reason why one should never run custom queries in place of the main query. I have explained everything in detail in the linked answer. – Pieter Goosen Mar 15 '16 at 10:13

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.