Set Posts Per Page For Any Archive Type

Use this code in your child themes functions file with a conditional tag to target a specific archive page type to set posts per page.

add_action( 'pre_get_posts', 'set_posts_per_archive_page' );
 
function set_posts_per_archive_page( $query ) {
 
if ( ! is_admin() && $query->is_main_query() ) {
 
    $query->set('posts_per_page', '5');
 
    }
}

The above code sets all archive pages to display 5 posts per page.

Post Per Page For Specific Category #

Only executes on the news category archive page.

add_action( 'pre_get_posts', 'set_posts_per_archive_page' );
 
function set_posts_per_archive_page( $query ) {
 
if ( ! is_admin() && $query->is_main_query() && is_category( 'news' ) ) {
 
    $query->set('posts_per_page', '1');
 
    }
}

Swap out the slug news in the above code to target a different category.

Swap out the 1 to change the number of posts per page you want to display on the archive page.

Was This Tutorial Helpful?

Free

$0

Access only to all free tutorials per month.



Monthly

$75

Access to 10 premium tutorials per month.


Tutorial Request


Includes code guarantee and coding support.

Yearly

$500

Access to 15 premium tutorials per month.


Monthly Tutorial Request


Includes code guarantee and priority coding support.