This issue is in my index.php (although i’m thinking about moving this code to home.php instead). I have a custom WP_Query which shows some posts per page, specifying post types (i have a custom type which the main query was not able to show). I have 42 posts atm (it’s the WordPress theme unit plus something i wrote earlier) and i did set 3 posts per page, which means i should have 15 pages. But from the 6th page and forward, i get a 404 error.
Code in index.php
<?php
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array(
'posts_per_page' => 3,
'paged' => $paged,
'post_type' => array('post', 'blog_post'),
);
$query = new WP_Query($args); ?>
<?php echo $query->max_num_pages ?>
<?php while ( $query->have_posts() ) : $query->the_post() ?>
<section class="article-container col-md-12">
<?php if (has_post_thumbnail()) the_post_thumbnail("post-thumbnail", array('class' => 'img-responsive col-md-5')); ?>
<article class="col-md-7">
<h3><a href="https://wordpress.stackexchange.com/questions/268311/<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
<?php the_excerpt(); ?>
<a href="https://wordpress.stackexchange.com/questions/268311/<?php the_permalink(); ?>" class="article-read-more">LEGGI TUTTO</a>
</article>
</section>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php if (function_exists("pagination")) {
pagination($query->max_num_pages);
} ?>
Pagination function (i customized it cause i wanted my markup)
function pagination($pages="", $range = 4) {
$showitems = ($range * 2)+1;
global $paged;
if(empty($paged)) $paged = 1;
if($pages == '') {
global $wp_query;
$pages = $wp_query->max_num_pages;
if(!$pages) {
$pages = 1;
}
}
if(1 != $pages) {
echo '<section class="text-center">';
echo '<ul class="pagination">';
if($paged > 2 && $paged > $range+1 && $showitems < $pages) echo "<li><a href="".get_pagenum_link(1)."">« First</a></li>";
if($paged > 1 && $showitems < $pages) echo "<li><a href="".get_pagenum_link($paged - 1)."">‹ Previous</a></li>";
for ($i=1; $i <= $pages; $i++) {
if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems )) {
echo ($paged == $i)? "<li class=\"active\"><a href=\"#\">".$i."</a></li>":"<li><a href="".get_pagenum_link($i)."">".$i."</a></li>";
}
}
if ($paged < $pages && $showitems < $pages) echo "<li><a href=\"".get_pagenum_link($paged + 1)."\">Next ›</a></li>";
if ($paged < $pages-1 && $paged+$range-1 < $pages && $showitems < $pages) echo "<li><a href="".get_pagenum_link($pages)."">Last »</a></li>";
echo "</ul>";
echo "</section>";
}
}
So, considering i cannot use the main query as it is and a solution with it doesn’t apply, what can i do? Any help will be appreciated.