1

I am displaying 2 posts in archive page with pagination. But my next page pagination link is redirecting to the home page instead of going to next page with containing next 2 posts.

The URL is showing like - https://domain.com/works/page/2/ but it is redirecting to the index page.

function.php

function pagination_bar( $custom_query ) {
    $total_pages = $custom_query->max_num_pages;
    $big = 999999999; // need an unlikely integer

    if ($total_pages > 1){
        $current_page = max(1, get_query_var('paged'));

        echo paginate_links(array(
            'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
            'format' => '?paged=%#%',
            'current' => $current_page,
            'total' => $total_pages,
        ));
    }
}

archive.php

$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array(
  'paged'         => $paged,
  'category_name' => 'works',
  'posts_per_page' => 2
);

$wrk = new WP_Query($args);

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

<a href="<?php the_permalink() ?>">
  <div class="inner_work">
    <div class="work_arc_img">
      <?php the_post_thumbnail(); ?>
    </div>
    <div class="work_card">
      <p class="in_work_title"><?php the_title(); ?></p>
    </div>
  </div>
</a>

<?php }  ?>

<nav class="pagination">
    <?php pagination_bar( $wrk ); ?>
</nav>

<?php wp_reset_postdata();?>
  • Welcome to WordPress Development. I hope you find the answer(s) you are looking for. Our site is different from most - if you have not done so yet, consider checking out the tour and help center to find out how things work. – Matthew Brown aka Lord Matt May 31 at 18:09
  • 1
    pagination APIs work off of the main query, but you're using a secondary query to replace, which doubles the number of queries in use. If you want to modify what the main query fetches, don't create a new query, use the pre_get_posts filter instead – Tom J Nowell May 31 at 20:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.