$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>404 error - Pagination (page2) displaying 404 on archive pages|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin10PV0评论

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.

Post a comment

comment list (0)

  1. No comments so far