Support » Fixing WordPress » Custom Post Types 404 with Pagination due to Reading Settings

  • Resolved Moondrop


    When using custom post types on my home page I receive 404’s on later paginated pages. (eg. pages 1-6 work perfectly, pages 7-11 give 404’s)

    I know the cause of this issue has to do with the Reading Settings>Blog pages show at most option. If the “Blog pages show at most” is greater than my “posts_per_page” number in my query I receive 404’s.

    The common solution to this seems to be setting the “Blog pages show at most” to 1, but I need the pagination to work properly without restricting the “Blog pages show at most” to 1 (for I am using infinite scroll, and the plugin stops retrieving posts when it hits a 404).

    Here is how my query currently looks:

    if ( get_query_var('paged') ) {
        $paged = get_query_var('paged');
    } else if ( get_query_var('page') ) {
        $paged = get_query_var('page');
    } else {
        $paged = 1;
    $type = 'events';
    	'post_type' => $type,
    	'post_status' => 'publish',
    	'paged' => $paged,
    	'posts_per_page' => 10,
    $wp_query = null;
    $wp_query = new WP_Query();

    Any help would be much appreciated.

Viewing 5 replies - 1 through 5 (of 5 total)
  • Hi hadi006,

    Thank you for the reply and link! I looked through the article and found this code tidbit which I had hoped to solve my problem:

    function events_posts_per_page( $query ) {
            if ( $query->query_vars['post_type'] == 'events' ) $query->query_vars['posts_per_page'] = 5;
            return $query;
        if ( !is_admin() ) add_filter( 'pre_get_posts', 'events_posts_per_page' );

    Unfortunately, even after adding in the code above, I am still receiving 404s and they are related to the “Blog pages show at most” setting.

    I found a solution to my issue. I was on the right track thanks to the article hadi060 linked, but missing just a bit more work.

    It seems I needed to modify the main query exclusively in my functions.php instead of setting it with my $args that I was placing on my home.php (I found the code I needed here).

    This is the code I added to my functions.php:

    if ( !is_admin() ) add_filter( 'pre_get_posts', 'my_get_posts' );
    function my_get_posts( $query ) {
    	if ( is_home() && $query->is_main_query() ){
    		$query->set( 'post_type', array( 'events' ) );
    		$query->set('posts_per_page', 8);                       
    	return $query;

    Happy to hear that, and thanks for sharing your solution here, Moondrop 🙂

    Moondrop‘s code really saved me! I finally managed to paginate on the homepage successfully for the first time. Now I’m facing the issue that I can’t go past page 2.

    Thank you for sharing!

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Custom Post Types 404 with Pagination due to Reading Settings’ is closed to new replies.