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/… Aug 17, 2020 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.
    – pjk_ok
    Aug 17, 2020 at 19:04

2 Answers 2

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.
    – pjk_ok
    Aug 17, 2020 at 19:02
  • Not sure why, but its working for me. I tested that in a WP custom template.
    – Awais
    Aug 17, 2020 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
    – pjk_ok
    Aug 17, 2020 at 22:17
0

I would use something like this :

$prev_link = get_previous_posts_link( '&#x000AB; ' . __( 'Previous Page' ) );
$next_link = get_next_posts_link( __( 'Next Page' ) . ' &#x000BB;' );

if ( $prev_link || $next_link ) {

$pagination  = $prev_link ? sprintf( '<div class="pagination-previous alignleft">%s</div>', $prev_link ) : '';
$pagination .= $next_link ? sprintf( '<div class="pagination-next alignright">%s</div>', $next_link ) : '';

echo $pagination:

}

Or

You could use the_posts_pagination()

Your Answer

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

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