Changing posts per page for a post type

By default, post type archives inherit the same posts per page amount as the Reading settings page and the “Blog pages show at most” setting. However, depending on your use case, you may need to change that value just for a given post type.

Utilizing the pre_get_posts action hook, you can easily modify the posts per page value for a given post type.

You will add the following snippets to your theme’s functions.php file or if you want, to a custom plugin built just for your own website.

Changing posts per page total

<?php
function my_cptui_change_posts_per_page( $query ) {
    if ( is_admin() || ! $query->is_main_query() ) {
       return;
    }

    if ( is_post_type_archive( 'movie' ) ) {
       $query->set( 'posts_per_page', 30 );
    }
}
add_filter( 'pre_get_posts', 'my_cptui_change_posts_per_page' );

In this code we check to see if WordPress is in the admin dashboard or not acting on the main query, and return early. If we are acting on the main query for the request, and are not in the admin, then we check to see if we are on the “movie” post type archive. If so, then we set the posts_per_page parameter to a total of 30 instead of the default 10.

The is_post_type_archive accepts an array of post types if you want to change the value for multiple post types.

Updated Dec 12, 2022 9:08 PM