WordPress Posts Per Page Per Custom Post Type

Published 5 years 3 months ago on May 16, 2011 — 1 min read

With Custom Post Types it’s quite likely that you’ll want to use a different number of posts per page in comparison to the Blog pages show at most setting found in Settings > Reading. I’d strongly suggest avoiding a solution involving the posts_per_page argument of a custom loop and instead use one of the many filters available to us:

// posts per page based on CPT
function iti_custom_posts_per_page($query)
{
    switch ( $query->query_vars['post_type'] )
    {
        case 'iti_cpt_1':  // Post Type named 'iti_cpt_1'
            $query->query_vars['posts_per_page'] = 3;
            break;

        case 'iti_cpt_2':  // Post Type named 'iti_cpt_2'
            $query->query_vars['posts_per_page'] = 4;
            break;

        case 'iti_cpt_3':  // Post Type named 'iti_cpt_3'
            $query->query_vars['posts_per_page'] = 5;
            break;

        default:
            break;
    }
    return $query;
}

if( !is_admin() )
{
    add_filter( 'pre_get_posts', 'iti_custom_posts_per_page' );
}

This snippet can be added in your theme’s functions.php

Of course, you’ll want to customize the Custom Post Type names in use, but this will allow the pagination to work properly based on a custom number of entries per page you’d like to show.

Copyright © 2006—2016 Jonathan Christopher