Number Of Custom Post Type Posts Per Page Pagination

I’ve spent the last couple of weeks building a template base I can work from and a lot of this has been within custom post types. Hit a snag yesterday, in that custom post types inherit the pagination of the blog pages.

After a lot of faffing around with conditional logic within the custom post type archive itself, it dawned on me that it’d be way more sensible to use a filter to handle it.

So here you go: set the number of custom post type posts per page; drop it into your functions.php file and change your-custom-post-type to your own.

// Set the number of a custom post type posts per page
add_filter( 'pre_get_posts', 'be_archive_query' );
function be_archive_query( $query ) {
if( $query->is_main_query() && $query->is_post_type_archive('your-custom-post-type') ) {
$query->set( 'posts_per_page', 8 );
}
}

Socially enable us...Tweet about this on Twitter0Share on Facebook0Share on Google+0Share on LinkedIn0Share on Reddit0Share on StumbleUpon0Pin on Pinterest0Share on Tumblr

Leave a Reply