Support » Fixing WordPress » Error 404 on pagination when changing posts_per_page on query_posts

  • Hi,
    please I need help resolving a pagination problem with wordpress. I currently using WP 3 RC1.

    The problem is that I get a 404 error when I go to page 2 of my archive after setting the posts_per_page to a value different from that set on admin settings page (lower value than that of settings).

    This is what I understood:
    If I set the value of posts_per_page variable on query_posts to a lower value than that set on the settings page than I will get the 404 error.
    Example: settings set to 10 posts per page [$ppp = get_option(‘posts_per_page’) will echo 10]
    posts_per_page set to 3 on query_posts
    If I have a total of 9 posts on my wordpress database then, with the settings above, page two will not show and I get the 404 error!

    This is because – I THINK but I may be wrong – wordpress still consider the 10 posts per page setting to determine IF the second page exists or not, completely ingnoring that I set posts_per_page to 3 on the query_posts and so even if there should be a total of 9/3 = 3 pages he calculates 9/10 = 1 page and so page 2 does not exists.

    This could be an explanation but what is the solution?

    I already read the codex pages and searched the forum and googled the internet but find only people with the same problem but no solution.

    The code I wrote is correct infact if I set the global setting of posts per page (from admin page) to the same value of that set on the archive page (via query_posts) [so that posts_per_page = get_option(‘posts_per_page’) = 3 for example) then everything works fine! And all the 3 pages works without problem (no 404 error).

    Please help me find a solution for this. I already wasted much time!

    Thanks

    PS Don’t just say it is because of WP 3 RC1: I had the same problem also on 2.9.2

Viewing 15 replies - 1 through 15 (of 15 total)
  • Thread Starter marinig

    (@marinig)

    Help please!

    Thread Starter marinig

    (@marinig)

    I did some debug and found out something interesting.
    I put at the end of the 404 template file a print_r($wp_query); and discovered that after clicking on the “next page” link on the first archive page, the posts_per_page variable is set back to the default value (that set on the admin settings page).
    In other words my setting of the posts_per_page variable written at the beginning of the archive template is completely ignored and somehow lost.
    Still I don’t know why this happens…

    It seems that the wp_query is executed before opening the archive template where there’s the “correct” custom posts_per_page value and so, considering the default value (higher than the custom one), page 2 is non existent and then 404 error is correct…

    I managed to solve the problem using a custom url rewrite rule (put on functions.php of my template) introducing the correct posts_per_page value directly on the query string at redirect and everything goes right. But this is not a solution as I hate to change 2 different files just to change the posts_per_page setting of the custom archive page. I think there should be another and hopefully better way to do this.

    Please, tell me something.
    Thanks

    Thread Starter marinig

    (@marinig)

    Up 🙂

    Thread Starter marinig

    (@marinig)

    I think this topic may interest many wordpress users. So please, help!

    You can see the bug in action:
    1) make a fresh install of your favorite versione of wordpress
    2) create a new category
    3) insert, say, 9 posts in this category
    4) go to admin > settings > reading and set to 10 the numer of posts showed on each page (global posts_per_page setting)
    5) Open archive.php in a text editor and write this code at the beginning:

    <?php
    global $query_string;
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    query_posts($query_string.'&paged='.$paged.'&posts_per_page=3');
    ?>

    6) Open the category on your browser and try to go on to second page and you will get the 404 error.

    Is there something wrong with the query?

    Thanks

    1) Login as the admin in WordPress.
    2)Go to the Settings tab.
    3)Go to the Reading tab.
    4)Change the Blog pages show at most from the default 10 to any number *less than 10. For example, 5.
    5)Save the changes.
    View the blog.
    Found this here and edited the number to less than 10, hope it helps you out. It fixed mine.

    oh.. and paste this in your functions file

    <?php
    /*
    Plugin Name: Category pagination fix
    Plugin URI: http://www.htmlremix.com/projects/category-pagination-wordpress-plugin
    Description: Fixes 404 page error in pagination of category page while using custom permalink
    Version: 2.0
    Author: Remiz Rahnas
    Author URI: http://www.htmlremix.com

    Copyright 2009 Creative common (email: mail@htmlremix.com)

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    GNU General Public License for more details.

    You are allowed to use, change and redistibute without any legal issues. I am not responsible for any damage caused by this program. Use at your own risk
    Tested with WordPress 2.7, 2.8.4 only. Works with wp-pagenavi
    */

    /**
    * This plugin will fix the problem where next/previous of page number buttons are broken on list
    * of posts in a category when the custom permalink string is:
    * /%category%/%postname%/
    * The problem is that with a url like this:
    * /categoryname/page/2
    * the ‘page’ looks like a post name, not the keyword “page”
    */
    function remove_page_from_query_string($query_string)
    {
    if ($query_string[‘name’] == ‘page’ && isset($query_string[‘page’])) {
    unset($query_string[‘name’]);
    // ‘page’ in the query_string looks like ‘/2’, so i’m spliting it out
    list($delim, $page_index) = split(‘/’, $query_string[‘page’]);
    $query_string[‘paged’] = $page_index;
    }
    return $query_string;
    }
    // I will kill you if you remove this. I died two days for this line
    add_filter(‘request’, ‘remove_page_from_query_string’);

    ?>

    @buritica

    I tryed out your plugin, but had the issue was still happening, I solved like this:

    http://wordpress.org/support/topic/pagination-with-custom-post-type-listing?replies=23#post-1637753

    Oh!!!! thank you so much buritica ! I confirm it works !

    I have exactly the same problem as marinig. I’m using WordPress 3.04 and it seems like posts_per_page setting is really set back to the default value during data processing.

    marinig, could you, please post here your solution from functions.php file? Really can’t understand how this could be done…

    Thanks in advance!

    … had the same problem on a tag page where I had to set posts_per_page to 1. Other solutions did not work …
    Ended up to use a hook in the get_option function (see: wp-includes/functions.php):

    function bti_change_posts_per_page(){ return 1; }
    if( preg_match("|\/".get_option('tag_base')."\/.+\/page\/[0-9]+$|i", $_SERVER['REQUEST_URI']) ){
    	add_filter( 'pre_option_posts_per_page' , 'bti_change_posts_per_page');
    }

    Code has to be placed in functions.php (as the tag.php template will not be loaded …).

    Works fine for me. Hope this helps …
    Cheeres, cz

    banesto

    (@banesto)

    while debugging i found out that default posts per page option should be less than any custom posts_per_page in query_posts function. That’s all. Weird, but that’s the fact.

    onti

    (@onti)

    have/had the exact same problem.
    the posts per page value in the wp backend was set to 5 and i used posts_per_page queries for my custom archive pages with 15 posts per page. as others have mentioned already the problem was that the paging gets screwed up. the paging recognizes the set value and shows me correctly the “earlier posts” links. the link itself brings me to 404 page. few tests showed me the internal value for the paging seems to be back to the default of 5.

    apparrently lots of developers are struggling with that issue and i couldn’t find any proper solution online. (didn’t test any plugins though..)

    my workaround is to reverse the values. so i set 15 as default value in the wp backend (for archives) and used posts_per_page 5 for my blog. luckily the paging problem didn’t occur this way…

    maybe this hint might be helpful for one…
    o.

    zuzya

    (@zuzya)

    banesto‘s solution helped me. After setting default posts per page option to 1 all works fine.

    codearts

    (@codearts)

    banesto thank you very much for your feedback – your absolutly right!

    Dont understand for what this behavoir is – but ok – if i know… no problem 🙂

    I’ve lost 6 hours on it!

    So if I want just one category with less posts than the blog default the solution is to change the default to the smaller amount and then change the query on all other templates!

    That’s an annoying bug!

Viewing 15 replies - 1 through 15 (of 15 total)
  • The topic ‘Error 404 on pagination when changing posts_per_page on query_posts’ is closed to new replies.