Below is my code on page_{slug}.php
. The first page is working fine, but after 1 page, I am getting 404 error for all pages. Pagination with the main query is working fine, but it does not support custom query. Please help.
<?php
$current_page = (get_query_var('paged')) ? get_query_var('paged') : 1;
$per_page = '2';
$testimonial_args = array(
'post_type' => 'blog_post',
'posts_per_page' => $per_page,
'paged' => $current_page,
);
$testimonials = new WP_Query($testimonial_args);
// echo "<pre>";
// print_r($wp_query);die;
?>
<?php if ($testimonials->have_posts()) : $i = 1; ?>
<?php while ($testimonials->have_posts()) : $testimonials->the_post(); ?>
<div class="col-lg-4 col-sm-6">
<div class="testimonial-card">
<div class="headline">
<?php the_title(); ?>
<span><?php echo the_title(); ?></span>
</div>
</div>
</div>
<?php $i+=1; ?>
<?php endwhile; ?>
<div class="pagination">
<?php
$big = 999999999; // need an unlikely intege
echo paginate_links( array(
'base' => str_replace( $big, '%#%.html', esc_url( home_url( '/<slug>/page/' . $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => 6
) );
?>
</div>
<?php else : ?>
<h2 class="center">Testimonials Not Found</h2>
<p class="center">Sorry, but there are no testimonials, check back later!</p>
<?php endif; ?>
Below is my code on page_{slug}.php
. The first page is working fine, but after 1 page, I am getting 404 error for all pages. Pagination with the main query is working fine, but it does not support custom query. Please help.
<?php
$current_page = (get_query_var('paged')) ? get_query_var('paged') : 1;
$per_page = '2';
$testimonial_args = array(
'post_type' => 'blog_post',
'posts_per_page' => $per_page,
'paged' => $current_page,
);
$testimonials = new WP_Query($testimonial_args);
// echo "<pre>";
// print_r($wp_query);die;
?>
<?php if ($testimonials->have_posts()) : $i = 1; ?>
<?php while ($testimonials->have_posts()) : $testimonials->the_post(); ?>
<div class="col-lg-4 col-sm-6">
<div class="testimonial-card">
<div class="headline">
<?php the_title(); ?>
<span><?php echo the_title(); ?></span>
</div>
</div>
</div>
<?php $i+=1; ?>
<?php endwhile; ?>
<div class="pagination">
<?php
$big = 999999999; // need an unlikely intege
echo paginate_links( array(
'base' => str_replace( $big, '%#%.html', esc_url( home_url( '/<slug>/page/' . $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => 6
) );
?>
</div>
<?php else : ?>
<h2 class="center">Testimonials Not Found</h2>
<p class="center">Sorry, but there are no testimonials, check back later!</p>
<?php endif; ?>
Share
Improve this question
edited Jun 14, 2020 at 5:00
Sally CJ
40.1k2 gold badges28 silver badges49 bronze badges
asked Jun 13, 2020 at 14:41
Satish GuptaSatish Gupta
1011 bronze badge
3
|
1 Answer
Reset to default 1I initially thought you're using the code in the question on a Page (a post of the type page
), but if you are actually using that code on an archive-based page (e.g. the home page, a category page, etc.), then the 404
error is normal. You can't paginate secondary/custom query on archive-based pages. Well, not when using the default paged
query string in the URL.
So a possible solution is use a Page and make your custom loop in there — basically, the code in the question would work after you made the changes I mentioned in the original answer. I.e.:
Change the
base
argument in yourpaginate_links()
call:// change from this 'base' => str_replace( $big, '%#%.html', esc_url( home_url( '/<slug>/page/' . $big ) ) ) // to this 'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) )
And in addition to that
base
, you also need to change the'total' => 6
to'total' => $testimonials->max_num_pages
. (more details here)
But if you actually wanted to customize the main query, then use the pre_get_posts
hook.
So for example, if your home page is set to show the latest posts, but you want to include only posts of the type blog_post
, then you can simply add this to your theme's functions.php
file:
add_action( 'pre_get_posts', function ( $query ) {
if ( ! is_admin() && $query->is_main_query() && is_home() ) {
$query->set( 'post_type', 'blog_post' );
$query->set( 'posts_per_page', 2 );
}
} );
And that's really all you need. I.e. No need to worry about the pagination because a good theme always includes a working pagination for the main query on the home page and other archive-based pages.
'total' => 6
to'total' => $testimonials->max_num_pages
. – Sally CJ Commented Jun 13, 2020 at 15:49