0

I have an archive page that pulls in posts using WP_Query(). On the homepage of the site it shows 16 of the custom post type posts, so on the archive I offset the archive page by 16 posts.

The code for this is:

$newsArticles = new WP_Query(array(
    'posts_per_page' => 16,
    'offset' => 16,
    'post_type'=> 'news'
));

while(  $newsArticles->have_posts()){
        $newsArticles->the_post(); ?>

        // HTML

<?php } ?>

However on this archive page the <?php echo paginate_links();?> function to show the pagination pages doesn't work. When I click on the page numbers or use the next and previous arrows, but it just shows the same posts on each page.

The pagination code I'm using is:

<p>
    <?php echo paginate_links(array(
        'prev_text' => 'NEWER',
        'next_text' => 'OLDER',
    ));?>
</p>

Does anybody know how I get the pagination to work with the WP_Query() property offset so the archive pagination behaves like a normal archive page (with pagination) ?

2
  • Note that the documentation for WP_Query explicitly states "Warning: Setting the offset parameter overrides/ignores the paged parameter and breaks pagination." There's also this Codex page with a possible solution: codex.wordpress.org/… – Jacob Peattie Aug 17 '20 at 4:28
  • Hi @JacobPeattie thanks for this. This codex example of how to fix this doesn't work either. Last year I was working with offset and pagination and nearly got it to work through looking at other answers on here. Now nothing is working at all. I'm wondering if the 5.5 update has broken something. – Paul12_ Aug 17 '20 at 19:04
0

WP_Query docs gives a warning about the offset parameter: Setting the offset parameter overrides/ignores the paged parameter and breaks pagination.

Try something like this:

 $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $newsArticles = new WP_Query(array(
        'posts_per_page' => 16,
        'post_type'=> 'news',
        'paged' => $paged,
    ));
while(  $newsArticles->have_posts()){
        $newsArticles->the_post(); ?>

        // HTML

<?php } ?>


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

See more examples of paginate_links here:

3
  • Hi Awais, this didn't work unfortunately. – Paul12_ Aug 17 '20 at 19:02
  • Not sure why, but its working for me. I tested that in a WP custom template. – Awais Aug 17 '20 at 20:33
  • @Awias, it doesn't take into account the offset it just shows all the posts from the beginning. I have 16 posts on a static home page and the archive page is supposed to continue from post 17 onwards – Paul12_ Aug 17 '20 at 22:17

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.