Archive page…limiting posts per page

Don’t create a new query and loop just to alter posts per page. Add a function hooked to pre_get_posts and alter whatever parameters you want in there before the query is run. This would go in your theme’s functions.php file, or a plugin.

function wpd_testimonials_query( $query ){
    if( ! is_admin()
        && $query->is_post_type_archive( 'testimonials' )
        && $query->is_main_query() ){
            $query->set( 'posts_per_page', 5 );
    }
}
add_action( 'pre_get_posts', 'wpd_testimonials_query' );

Then in the template you run the normal loop and pagination works as expected.

Leave a Comment