0

I have an archive.php template which I only use for the blog.

The code goes a bit like this:

<?PHP
  $the_query = new WP_Query( array(
    'posts_per_page'=>9,
    'post_type'=>'blog',
    'paged' => get_query_var('paged') ? get_query_var('paged') : 1) 
   ); 
?>

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

Title, text, etc pulled through

<?php endwhile; ?>

<?php
    $big = 999999999; // need an unlikely integer
    echo paginate_links( array(
        'base' => str_replace( $big, '%#%', get_pagenum_link( $big ) ),
        'format' => '?paged=%#%',
        'current' => max( 1, get_query_var('paged') ),
        'total' => $the_query->max_num_pages
    ) );
?>

<?php wp_reset_postdata(); ?>

But then I go to Page 2, my 404 page displays instead.

2
  • 1
    Don't write a new query, filter pre_get_posts instead. We have many solutions for that on our site.
    – fuxia
    Commented Nov 17, 2020 at 12:00
  • As fuxia says, use pre_get_posts, it will be faster and pagination will just work out of the box. If you ever need to change the posts the main query brings in, use that filter don't create a brand new replacement query then faff around with custom paginate_links calls and queries. Likewise if you have an archive for a custom post type, archive-blog.php will do that for you too
    – Tom J Nowell
    Commented Nov 17, 2020 at 12:32

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.