I'm using WP_Query to generate a custom loop on my blog page. My current blog page is assigned to a page called "Blog" and this page is using the default template file (index.php).
Here's a piece of code for my index.php file:
<?php $paged = ( get_query_var('paged')) ? get_query_var('paged') : 1; $args = array( 'posts_per_page' => 2, 'paged' => $paged ); $consulta = new WP_Query($args); if($consulta->have_posts()) : while ($consulta->have_posts()) : $consulta->the_post(); get_template_part('content', get_post_format()); endwhile; wp_reset_postdata(); ?> <div class="pagination text-left"> <?php next_posts_link("<< Older Posts", $consulta->max_num_pages) ?> </div> <div class="pagination text-right"> <?php previous_posts_link("Newer Posts >>"); ?> </div> <?php else: ?> <p>Nothing here</p> <?php endif; ?>
As you can see, I'm using the $paged variable and also retrieving max_num_pages from the query result.
I have 11 posts on the database. So, with 2 posts per page, I would expect to see 6 pages, but I only get 3 pages. The 4th page shows me a 404 error.
That code was working fine with WP 4.6 (I'm not quite sure). Now with 4.7, 4.72 etc, it's not working anymore.
I'm using a custom theme, but I tried also switching the theme for twenty_fifteen and editing its index.php file. Same result...
It seems like the posts_per_page argument is defaulting to the value used on "Settings->Reading" ("Blog pages show at most"), which at this moment is 5.
I have a template file called 404.php. I found out that when I delete it, the pagination works the way it should, showing 6 pages. Maybe because when there is not a specific 404 template file, it automatically falls back to index.php.
So, what is wrong with this 404.php file? I tried also with the native themes, using their 404 template file, and I get the very same error...