WordPress Posts Per Page Per Custom Post Type

Published 2 years 10 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.

There's a conversation brewing

  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. I’ve had some issues with with WP_Query and pagination, but clearing the global $wp_query fixed it.

    I’m working on something now that uses the paginate_links() function for posts, and possibly for post types. This may make a perfect addition if they decide to switch the number of total posts for the post type.

    Thanks!

  3. 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.

  4. 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.

  5. The theme is on GitHub. Specific template is: https://github.com/devinsays/portfolio-press/blob/master/archive-portfolio.php

    It’s a weird issue because the template works when set on a page, or the archive. It just breaks when set as the home page in the reading options.

    • 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;

    • 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?

  6. 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.

    • 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.

    • 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

  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. 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.

By all means, contribute

Leave a comment

Powered by Fusion

This article is so meta

Published May 16th, 2011

Random article

css.php