2
votes

I am having trouble with Wordpress pagination. I have a custom archive page displaying custom post types from the specific category. I want to use pagination and display 12 posts per page. My problem is that pagination works correctly but only up to the 8th page. After that I am being presented with "Page not found".

I am using theme built-in function to display page navigation (1, 2... 10, 11). It correctly shows 11 pages in total, but they seem not to work after the 8th page.

$taxonomy = 'product_cat';
$term_id = get_queried_object()->term_id;
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
     'post_type' => 'product',
     'paged' => $paged,
     'posts_per_page' => '12',
     'tax_query' => array(
          array(
               'taxonomy' => 'product_cat',
               'field' => 'term_id',
               'terms' => $term_id
          )
     )
);
<?php $wp_query = new WP_query( $args ); ?>
<?php if( $wp_query->have_posts() ): ?>
    <?php while( $wp_query->have_posts() ): ?>
        <?php $wp_query->the_post(); ?>
        //post content
    <?php endwhile; ?>
<?php endif; ?>

<?php s7upf_paging_nav();?>

<?php wp_reset_postdata(); ?>

@edit

When I go to a different category page which should have 12 pages they work correctly up to the 9th page. If I go to the one that has 2 pages, only the first one works.

I tried updaing permalinks. Setting posts per page to 12 in wordpress settings.

When i change posts per page in my query args to -1 it correctly shows all the posts on one page.

When manually setting the number of a page to display ('paged' => '11') it also displays correct page with correct posts.

1
With 12 posts per page and 11 pagination count, you should be having at least 121 post of your custom post type. Just want to confirm.zipkundan
@zipkundan yes, I have 125 posts in totalMike
For testing purpose can you try echo($wp_query->post_count)? that will show you how may posts are returned y the query and probably help you troubleshoot further.zipkundan
@zipkundan post_count shows 12 as set in query argsMike
are you using any plugin for this s7upf_paging_nav?Vel

1 Answers

2
votes

You are using the wrong query. On page you are creating your own query instead of the actual query the page already did. This will also be better for performance.

Step 1. Check what is in the normal query.

At the top of taxonomy-product_cat.php

global $wp_query;
var_dump( $wp_query->query_vars );

That probably fits mostly.

Step 2. Do the normal loop

Remove all your query stuff (maybe keep a backup of the $args for the next step)

example: Replace

<?php if( $wp_query->have_posts() ): ?>

with

<?php if( have_posts() ): ?>

And so on.

Step 3. Edit the main query

We are going to use the hook pre_get_posts

add_action('pre_get_posts', 'so_53315648');
function so_53315648( WP_Query $wp_query ){
    // only check this on the main query
    if (! $wp_query->is_main_query() ){
        return;
    }
    // only check this on the product_cat taxonomy
    if ( ! $wp_query->is_tax('product_cat')) {
        return;
    }
    // maybe do some more checks?

    $wp_query->query_vars['posts_per_page'] = 12;

    // Is this really needed??
    //$wp_query->query_vars['posts_type'] = 'product';

    // tweak the query the way you like.
}

As you can see $wp_query->query_vars should pretty much be the same $args. But do not overwrite it. This might break other stuff.

Of course I could not test your specific site. But the answer should be inside the pre_get_posts hook. And tweaking the main query instead of doing a extra separate one.

Test, also check the var_dump of step 1 that your changes are coming through. The checks at the top are to stop other pages from being affected, maybe you need more?

Let me know.