• When I click on the 2, 3 or any next page i get the 404 error. What’s wrong?

    Here’s my code:

    <?php query_posts('cat='.$cat.'&posts_per_page=4&paged=' . $paged); ?>
    <?php $posts = get_posts('cat='.$cat.'&posts_per_page=4&offset=0&paged=' . $paged); foreach ($posts as $post) : start_wp(); ?>
    <?php static $count2 = 0; if ($count2 == "5") { break; } else { ?>
    
    				<div class="poj_aktu" id="post-<?php the_ID(); ?>">
    <?php getImage('1'); ?>
    					<a href="<?php the_permalink() ?>"><?php the_title(); ?></a>
    					<p><?php the_content_rss(); ?></p>
    <div class="clear"></div>
    					<div class="info">
    						Dodał: <b><?php the_author(); ?></b>     Data: <b><?php the_time('j F Y') ?></b><div class="czyt">Czytań: <b><?php if(function_exists('the_views')) { the_views(); } ?> </b></div>
    					</div>
    				</div>
    
    <?php $count2++; } ?>
    <?php endforeach;?>

Viewing 2 replies - 1 through 2 (of 2 total)
  • I’ve found the same exact problem now (9 months later) on WP 3.2.1.
    If you set a posts_per_page value in a template file, lower then the one specified in the backend, you get a 404 error on pagination. If I’m not wrong, the 404 error shows on the last page (the 2nd when you have only few posts).

    After some debugging, I think the problem is that the var posts_per_page get lost going to page 2 and so WP uses the default one to decide if there are posts to show or not.
    Some examples:
    —————————————————————-
    SET A GREATER VALUE
    a) you have 9 posts in a particular category;
    b) you have “5” as posts_per_page setted in the backend;
    c) you specify “7” in your template file;

    First page:
    WP reads the template file, set the var with the new value (7) so it shows posts from 1 to 7

    Second Page:
    when you click on “next page”, WP lost your new posts_per page value and thinks: “ok, I’ve got 9 posts in this category, I’m on page 2 and I have to show 5 post per page, so this page exists for sure and I must load posts from 6 to 9!”
    Then it loads the template file, your var is setted again to 7, page number is 2, so WP loads and shows (correctly) posts 8 and 9.
    No more post to show so you don’t have a next page link to go further.

    SET A LOWER VALUE
    a) you have 9 posts in a particular category
    b) you have “5” as posts_per_page setted in the backend
    c) you specify “3” in your template file

    First page:
    wp reads the template file, set the var with the new value (3) so it shows posts from 1 to 3.

    Second Page:
    when you click on “next page”, WP lost your new posts_per page value and thinks: “ok, I’ve got 9 posts in this category, I’m on page 2 and I have to show 5 post per page, so this page exists for sure and I must load posts from 6 to 9
    Then it loads the template file, your var is setted again to 3, page number is 2, so WP loads and shows (correctly) posts from 4 to 6.

    Third page:
    when you click on “next page”, WP lost (again!) your new posts_per page value and thinks: “ok, I’ve got 9 posts in this category, I’m on page 3 and I have to show 5 post per page, so this page can’t exist!!!”
    Then it loads the 404 template file, your are very angry ang go out for a walk!
    —————————————————————-

    So, if you set a value greather then the one specified in the backend, even if the variable get lost and the default is used, WP calculates (mistaken for excess) thar there are certainly some other posts to show. This is not problematic, as the code on template file corrects it and doesn’t allow to go to next page if all posts are showed.
    If you specify a lower value, WP is “consuming” your remaining posts faster then your template file, that prints a link to next page and you get a 404 error.

    Ok. that’s a funny story. And now? My workaround is:

    function cure_wp_amnesia_on_query_string($query_string) {
    	switch ( $query_string['category_name'] ) {
    		case 'category-slug-1':
    		case 'category-slug-2':
    			$query_string['posts_per_page'] = 5;
    			break;
    		case 'category-slug-3':
    			$query_string['posts_per_page'] = 6;
    			break;
    		case 'category-slug-4':
    			$query_string['posts_per_page'] = 3;
    		//default:
    			break;
    	}
    	if ( isset( $query_string['s'] ) ){//case SEARCH-PAGE
    		$query_string['posts_per_page'] = 5;
    	}
    	return $query_string;
    }
    add_filter('request', 'cure_wp_amnesia_on_query_string');

    Add this function in your functions.php and set here the number of posts you want per category. I’ve not tried it with CPT but I suppose that we can find a specific index in $query_string to test against as I do with ‘category_name’ and ‘s’.

    Hope this can help someone.

    Please go here. I’ve correct some issues in the explanation and in the code.

    Thanks.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘query posts pagination 404’ is closed to new replies.