Will McLean
Process diary

Correctly setting a different number of posts per page on a WordPress archive page

Last modified: October 31, 2017
Created: July 30, 2017

I found out recently that you should not be using a custom WP_Query on an archive page if you don’t want to cause major issues with your pagination. However, for this particular Custom Post Type archive page I needed to set a different number of posts per page than was set in the admin settings for the blog page. Without using a WP_Query I couldn’t find how to do this until I discovered the pre_get_posts action. So in my functions.php file I just added the code below:

function locations_posts_per_page( $query ) {
    if ( is_post_type_archive( 'locations' ) && !is_admin() ) {
        $query->set( 'posts_per_page', 500 );
add_action( 'pre_get_posts', 'locations_posts_per_page', 1 );

This is a Knowledge Base post.