404 error - Pagination (page2) displaying 404 on archive pages

admin2025-06-04  1

I have a custom WP_Query inside of an archive. I know this is not ideal, but when I try switching it out for a pre_get_posts option, then my page just enters an infinite loop, so I'd rather stick with the WP Query. The problem is that pagination sends me to a 404 error on /page/2.

My query (it also has some taxonomy and meta queries added later)

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

  $query_args = array(
   'post_type' => 'product',
   'posts_per_page' => 2,
   'paged' => $paged,
  );

Here is my pagination function

function pagination($query){ ?>
  <ul class="pagination">
    <?php
        $pages = paginate_links( array(
            'base'         => str_replace( 999999999, '%#%', esc_url( get_pagenum_link( 999999999 ) ) ),
            'total'        => $query->max_num_pages,
            'current'      => max( 1, get_query_var( 'paged' ) ),
            'format'       => '?paged=%#%',
            'show_all'     => false,
            'type'         => 'array',
            'end_size'     => 2,
            'mid_size'     => 1,
            'prev_next'    => true,
            'prev_text'    => '<span class ="fa fa-caret-left" aria-hidden="true"></span><span class="prev-text">Prev</span>',
            'next_text'    => '<span class="next-text">Next</span> <span class ="fa fa-caret-right" aria-hidden="true"></span>',
            'add_args'     => false,
            'add_fragment' => '',
        ) );

      if (is_array($pages)):
        foreach ($pages as $p): ?>
          <li class="pagination-item js-ajax-link-wrap">
            <?php echo $p; ?>
          </li>
        <?php endforeach;
      endif; ?>
  </ul>
<?php
}

The pagination displays properly...the problem is when I go to "/page/2" it throws a 404 error.

I have a custom WP_Query inside of an archive. I know this is not ideal, but when I try switching it out for a pre_get_posts option, then my page just enters an infinite loop, so I'd rather stick with the WP Query. The problem is that pagination sends me to a 404 error on /page/2.

My query (it also has some taxonomy and meta queries added later)

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

  $query_args = array(
   'post_type' => 'product',
   'posts_per_page' => 2,
   'paged' => $paged,
  );

Here is my pagination function

function pagination($query){ ?>
  <ul class="pagination">
    <?php
        $pages = paginate_links( array(
            'base'         => str_replace( 999999999, '%#%', esc_url( get_pagenum_link( 999999999 ) ) ),
            'total'        => $query->max_num_pages,
            'current'      => max( 1, get_query_var( 'paged' ) ),
            'format'       => '?paged=%#%',
            'show_all'     => false,
            'type'         => 'array',
            'end_size'     => 2,
            'mid_size'     => 1,
            'prev_next'    => true,
            'prev_text'    => '<span class ="fa fa-caret-left" aria-hidden="true"></span><span class="prev-text">Prev</span>',
            'next_text'    => '<span class="next-text">Next</span> <span class ="fa fa-caret-right" aria-hidden="true"></span>',
            'add_args'     => false,
            'add_fragment' => '',
        ) );

      if (is_array($pages)):
        foreach ($pages as $p): ?>
          <li class="pagination-item js-ajax-link-wrap">
            <?php echo $p; ?>
          </li>
        <?php endforeach;
      endif; ?>
  </ul>
<?php
}

The pagination displays properly...the problem is when I go to "/page/2" it throws a 404 error.

Share Improve this question asked Mar 23, 2018 at 23:07 Jordan CarterJordan Carter 2912 gold badges5 silver badges13 bronze badges 1
  • Related: wordpress.stackexchange/questions/209693/… – Jesse Nickles Commented Jan 3, 2024 at 8:23
Add a comment  | 

2 Answers 2

Reset to default 8

Found the answer!!!

Put this in functions.php (or a required file). Of course, change it to suit your needs. I needed something that only worked for product category archives.

function modify_product_cat_query( $query ) {
  if (!is_admin() && $query->is_tax("product_cat")){
       $query->set('posts_per_page', 2);
  }
}
add_action( 'pre_get_posts', 'modify_product_cat_query' );

I also took out the posts_per_page parameter from my WP_Query.

Also after I've disabled plugin of orbisius simple shortlink problem solved! it override site/page/123 links to be redirected to page ID.

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

最新回复(0)