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.

2 Answers 2

6

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.

0
0

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

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.