WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Custom Post Types 404 with Pagination due to Reading Settings (6 posts)

  1. Moondrop
    Member
    Posted 2 years ago #

    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';
    $args=array(
    	'post_type' => $type,
    	'post_status' => 'publish',
    	'paged' => $paged,
    	'posts_per_page' => 10,
    );
    $wp_query = null;
    $wp_query = new WP_Query();
    $wp_query->query($args);

    Any help would be much appreciated.

  2. Abdelhadi
    Member
    Posted 2 years ago #

  3. Moondrop
    Member
    Posted 2 years ago #

    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.

  4. Moondrop
    Member
    Posted 2 years ago #

    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;
    }
  5. Abdelhadi
    Member
    Posted 2 years ago #

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

  6. Danimt
    Member
    Posted 1 year ago #

    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!

Topic Closed

This topic has been closed to new replies.

About this Topic