2

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.

EDIT

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...

  • 1
    to chnage the number of posts per page for the main query, have you read codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts ? – Michael Feb 21 '17 at 1:14
  • I tried to use this solution, but it's not working yet. See my edit. – Marcelo Xavier Vieira Feb 21 '17 at 14:33
  • 1
    WordPress decides a page is a 404 based on the results of the Main Query, before the template is loaded. That's how it knows to load the 404 template. Nothing you do in the template will change the results of the Main Query- you can't paginate an archive page beyond the number of pages in the Main Query. You need to alter the Main Query via pre_get_posts, not create a new query in the template. – Milo Feb 21 '17 at 15:37
  • Ok, now I see what I'm doing wrong. I thought I could override the main query when I used a new instance of that query with the WP_Query object. It turns out that the main query is still valid. That's why I see only 3 pages, not 6. I deleted the custom query and wrote a single loop instead. Then I used pre_get_posts as Milo and @Michael told me. It's working! Thank you, guys! – Marcelo Xavier Vieira Feb 21 '17 at 19:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.