WordPress Posts Per Page Per Custom Post Type

Posted: May 16, 2011 Comments(27)

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.

Get my newsletter

Receive periodic updates right in the mail!

  • This field is for validation purposes and should be left unchanged.

Comments

  1. Very nice idea! I typically just set it (hard code) on a per custom post type basis in the query_posts function, but if I ever need multiple custom types I’ll be sure to implement something similar.

  2. An interesting approach. I like it … just cannot think where I would put it to use at the moment but worth keeping a bookmark for later reference.

  3. Hey Chris.

    I’m using a method Mark Jaquith suggested: http://markjaquith.wordpress.com/2010/07/19/wordpress-question-answer-july-19th-2010/

    Both your snippet and his work fine if you’re looking at the native custom post type archives, but I also have a page template that shows custom post types, and that gave me a “Notice: Undefined index: post_type” when using your snippet.

    I was looking at this because I have a paging issue in my Portfolio theme: http://wordpress.org/extend/themes/portfolio-press. It only happens when you are using the template to display the custom post types, and set that template as your front page.

    Guess I’ll have to dig into a bit more.

  4. Fixed it. Had to add this code back in for paging fixes:


    // WP 3.0 PAGED BUG FIX
    if ( get_query_var('paged') )
    $paged = get_query_var('paged');
    elseif ( get_query_var('page') )
    $paged = get_query_var('page');
    else
    $paged = 1;

  5. Hi, I have built a custom theme and need to use the code you have suggested above; however I don’t have a functions.php page and Im not sure what that page should contain. Would love any help anyone wants to give this newbie!
    Thanks.

  6. Sure thing — functions.php needs to reside in your theme directory. The code used above can be pasted there and it will be automatically executed by WordPress at runtime.

  7. Thank you so much for this. I’ve had pagination problems left, right and center with my custom post type. I’ve always found that the “blog pages show at most” setting in the admin always seems to overwrite it no matter what I did, and could never find a solution anywhere else.

  8. I love this, but what about a situation where I want to display or example three latest posts from a custom post type in my footer, but ten on an overview page for posts of that same type? Is there a way that could be made possible with this?

  9. So, I have tried this several ways and still can’t get it to work!
    I have taken the code as it is posted about and put it in a functions.php page in the theme directory.

    If my category name is awards how should it read this is one of the ways I have tried it.
    Does there need to be a tag on the category as well?
    case ‘iti_cpt_awards’: // Post Type named ‘iti_cpt_awards’

    Thanks

  10. Thanks, this is in the right direction for a solution that I need to a custom category archive template I’m working on with infinite scrolling. Instead of filtering by CPT, I’m going to look into a filter for categories.

  11. I’m seeing the same thing:

    Debug: Undefined index: post_type on line XXX of /wp-content/themes/XXX/functions.php

    Is there any sign of a fix for this?

  12. Thank you SO much!

    It took me almost a week to figure out why the custom queries would not play nice with page numbers and number of posts. This solution helped get it to work.

  13. Hello this is great and works well, I have just one problem, I have widget shows one post from CPT and if I use this code there are all of them, what can I do to fix it? Thank you! M

Leave a Reply

Your email address will not be published. Required fields are marked *