WordPress Pagination not displaying posts after certain page

WordPress determines if a paginated page exists based on the results of the main query. Each page is actually querying 6 posts, while your custom query only loads 3.

The solution – Don’t create a new query in the template, use pre_get_posts to modify the main query via your theme’s functions.php

function wpa90437_media_category( $query ) {
    if ( $query->is_category('media') && $query->is_main_query() ) {
        $query->set( 'posts_per_page', 3 );
    }
}
add_action( 'pre_get_posts', 'wpa90437_media_category' );

Then just run the normal loop in your template.

Leave a Comment