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.
Comments
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.
Did you ever find that to mess with the pagination in any way?
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!
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.
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.
[…] If you’d like to have a custom number of posts per page, you can do that too. […]
I’d love to see some code samples (specifically the troublesome page template) and I might be able to provide some more detailed insight.
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;
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.
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.
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?
[…] method came to me by way of Jonathan Christopher‘s post called WordPress Posts Per Page Per Custom Post Type. Thank’s, […]
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
[…] method came to me by way of Jonathan Christopher‘s post called WordPress Posts Per Page Per Custom Post Type. Thank’s, […]
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.
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?
[…] Had a custom template that I set as the Front Page for a site but couldn’t get pagination working correctly. Fortunately found a fix thanks to Devin Price (@devinsays) over on a blog at https://jonchristopher.us/20110516/wordpress-posts-per-page-per-custom-post-type/#comment-42451 […]
[…] My references: Sort by custom taxonony Add a switch for each custom post type […]
3 years later…your trick still working and it’s SO useful !
Thanks Jonathan !!
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.
It’s an old post, but this helped me solve an issue today – thanks!
Glad it was helpful!
Dude, you are a BEAUTY – works perfectly, even in March 2015. Pop a skirt on and I’d marry you in a heartbeat.
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
Just found this. So helpful, thank you! Still works like a champ!