Pagination shows 404 after a certain number of pages

The problem is that WordPress is executing the main query before your custom query (and the main query is based on the default post type only).

You can intercept the main query, modify it, and then use it like so

function add_blog_post_to_query( $query ) {
    if ( $query->is_home() && $query->is_main_query() ) {
        $query->set( 'post_type', array('post', 'blog_post') );
        $query->set( 'posts_per_page', 3 );
    }
}
add_action( 'pre_get_posts', 'add_blog_post_to_query' );

Then if you want to still use your custom pagination function you would call it like so

if ( function_exists( 'pagination' ) ) {
    global $wp_query;
    pagination( $wp_query->max_num_pages );
}

Now, instead of using your custom query, you can use the standard functions like so:

if ( have_posts() ) :

    /* Start the Loop */
    while ( have_posts() ) : the_post();

        // your markup here

    endwhile;

else :

    // no posts found

endif;

Leave a Comment