I'm trying to modify the archive query of archive page with this function hooked to pre_get_posts
action hook.
// Load our function when hook is set
add_action( 'pre_get_posts', 'rc_modify_query_limit_posts' );
function rc_modify_query_limit_posts( $query ) {
// Check if on frontend and main query is modified
if( ! is_admin() && $query->is_main_query() && $query->is_archive() ) {
$query->set('posts_per_page', '9');
}
This is working perfectly, but somehow messes up my function for displaying current posts displayed"
function archive_post_count() {
global $wp_query;
$showing = '';
if ( $wp_query->found_posts > 1 ) {
$page_number = is_paged() ? $wp_query->query_vars['paged'] : 1;
$current_max = ( get_option( 'posts_per_page' ) * ( $page_number - 1 ) ) + $wp_query->post_count;
$current_min = $current_max - $wp_query->post_count + 1;
$range = ( $current_min == $current_max ) ? strval( $current_min ) : $current_min . '-' . $current_max;
$total = $wp_query->found_posts;
$showing = 'Shown ' . $range . ' from ' . $total . ' posts ';
}
echo $showing;
}
Currently I have 11 posts and on first page everything works OK. But when I hit page 2 "Shown 11-12 from 11 posts" message is shown. Where is my mistake?