custom post types - Pagination Not Working When Used With WP_Query() `offset` Property

admin2025-01-07  4

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) ?

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) ?

Share Improve this question asked Aug 17, 2020 at 4:00 pjk_okpjk_ok 9082 gold badges15 silver badges35 bronze badges 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 Commented 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 Commented Aug 17, 2020 at 19:04
Add a comment  | 

2 Answers 2

Reset to default 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:

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()

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736252922a85.html

最新回复(0)