Published on Nov 17, 2018

How to change posts per page on a CPT archive

Sridhar Katakam

In the members-only forum, a user asked:

Hi,
If I wish to use Easy posts for a Custom post type Is it possible to

do Pagination (different from the reading list)
Have a search form
Would really appreciate a tutorial for this

This tutorial provides the steps to set a specific number of posts per page to appear on a portfolio Custom Post Type’s archive page which is different from the number set for archives in general at Settings > Reading > Blog pages show at most.

Step 1

Install and activate Code Snippets plugin.

Step 2

Go to Snippets > Add New.

Title: Set the number of items per page on Portfolio CPT Archive

Code:

add_filter( 'pre_get_posts', 'custom_change_portfolio_posts_per_page' );
/**
* Change Posts Per Page for Portfolio Archive.
* 
* @param object $query data
*
*/
function custom_change_portfolio_posts_per_page( $query ) {
if ( $query->is_post_type_archive( 'portfolio' ) && ! is_admin() && $query->is_main_query() ) {
$query->set( 'posts_per_page', '6' );
}
return $query;
}

In the above, replace portfolio with the name of your CPT.

and change 6 to your desired number of posts per page.

Source: https://www.billerickson.net/customize-the-wordpress-query/

tagschevron-leftchevron-rightchainangle-rightangle-upangle-downfolder-omagnifiercrossmenuchevron-downarrow-right