Hello,
I've created a custom post type called "testimonials" and I'm getting 404 errors when setting the post per page lower then the default blog pages show at most in settings > reading.
I'm wanting to only show 4 testimonials per page but the pagination links go to 404 error pages. If I set the default blog pages show at most to lets say "1" it works as expected. I do not want to change this to 1 because I already have our blog showing 10 posts.
I've tried resetting rewrite rules, saving permalinks and I do not have another page with the same slug.
Help would greatly be appreciated.
<?php
$current_page = (get_query_var('paged')) ? get_query_var('paged') : 1;
$per_page = '2';
$testimonial_args = array(
'post_type' => 'testimonials',
'posts_per_page' => $per_page,
);
$testimonials = new WP_Query($testimonial_args);
?>
<?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 types_render_field('field-of-study'); ?></span>
</div>
<div class="card-container">
<div class="photo"><?php the_post_thumbnail('thumbnail'); ?></div>
<p><?php echo get_excerpt(260); ?></p>
<a href="<?php the_permalink(); ?>" class="btn btn-secondary btn-pill hvr-float-shadow"><i aria-hidden="true" class="fa fa-arrow-circle-o-right"></i> Full Story</a>
</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, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $testimonials->max_num_pages
) );
?>
</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; ?>
Hi Derek,
it's one of these WordPress issues, which are known since years but still not resolved.
But I have a workaround for you, but first we have to do a little fix to your query:
Change:
$testimonial_args = array(
'post_type' => 'testimonials',
'posts_per_page' => $per_page,
);
To:
$testimonial_args = array(
'post_type' => 'testimonials',
'posts_per_page' => $per_page,
'paged' => $current_page,
);
Now the fix for the WordPress bug... add to your themes functions.php:
function toolset_fix_custom_posts_per_page( $query_string ){
if( is_admin() || ! is_array( $query_string ) )
return $query_string;
$post_types_to_fix = array(
array(
'post_type' => 'testimonials',
'posts_per_page' => 2
),
// add another if you want
/*
array(
'post_type' => 'movie',
'posts_per_page' => 2
),
*/
);
foreach( $post_types_to_fix as $fix ) {
if( array_key_exists( 'post_type', $query_string )
&& $query_string['post_type'] == $fix['post_type']
) {
$query_string['posts_per_page'] = $fix['posts_per_page'];
return $query_string;
}
}
return $query_string;
}
add_filter( 'request', 'toolset_fix_custom_posts_per_page' );
Best,
Christian
Thank you, this now works as expected. I'll use this moving forward for all of my projects 🙂
Much appreciated!