Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have a weird problem I'm having hard time understanding.

Pagination gives me error 404 when I click page #3 when there is only 1 post on that page. If I add 1 more so that on page #3 there are minimum 2 posts error goes away!

Any way to diagnose this issue?

My code is this:

                //default values
                if ( !isset ($_POST['lolpp']) ) { $listing_order_logic_perpage = 'date'; }
                if ( !isset ($_POST['lopp']) ) { $listing_order_perpage = 'DESC'; }
                if ( !isset ($_POST['lpp']) ) { $listing_per_page = 6; }
            ?>

            <!-- End #catlistfilter --></div>








        <ul class="infoslide">

                <?php
                        //calculating offset depending on number of featured listing, lets create variable first
                        $count_featured_offset = 0;


                    //featured listing loop
                    query_posts( 'order=DESC&orderby=date&posts_per_page=2&post_type=post&cat='.$current_category_id.'&meta_key=featured&meta_value=yes' );
                    if ( have_posts() ) : while ( have_posts() ) : the_post();
                    //lets save ID of listing which is featured to later exclude it from normal loop
                    $do_not_duplicate[] = $post->ID;
                ?>

                        <li class="featuredinfoslide">
                            <div class="sponsoredTag"></div>


                                         <a href="<?php the_permalink(); ?>">
                                            <?php 
                                                if ( has_post_thumbnail() ) {
                                                    echo get_the_post_thumbnail($post->ID, 'thumbnail', array('alt' => the_title('','',false), 'title' => get_the_excerpt(), 'class' => 'primarythumb' ));
                                                } else {
                                                    echo '<img width="120" height="68" class="primarythumb" alt="'.the_title("","",false).'" title="'.get_the_excerpt().'" src="'.get_bloginfo('template_url').'/thumbs/post-thumb-na.png">';
                                            } ?>
                                         </a>

                                      <h3><a class="title" href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>

                                    <?php
                                      echo "<p>".__(get_post_meta($post->ID, 'Address', true))."</p>";
                                      echo '<span class="telephonebtn">'.get_post_meta($post->ID, 'Phone', true).'</span>';

                                        $link = get_post_meta($post->ID, 'url', true);

                                        if ( $link ) {
                                          echo '<span class="wwwbtn wwwon"><a href="'.$link.'" target="_blank">'.__('Official Website','holidayge').'</a></span>';
                                        } else {
                                          echo '<span class="wwwbtn wwwoff"><a href="#">'.__('Official Website','holidayge').'</a></span>';
                                        }
                                    ?>

                                      <p class="infoslidedescrpt"><?php echo trim(strip_tags($post->post_excerpt)); ?></p>


                                                    <ul class="reviewsdetail">
                                                        <li class="reviewsrightborder">
                                                            <?php echo ( wp_gdsr_rating_article()->votes ); ?> <?php _e('users rated','holidayge'); ?>
                                                            <div><?php if(function_exists('wp_gdsr_render_article')){ wp_gdsr_render_article();}?></div>
                                                        </li>
                                                        <li class="reviewsleftborder">
                                                            <?php _e('Number of reviews','holidayge'); ?>
                                                            <span><a href="<?php echo get_permalink();?>#comments"><?php comments_number( '0', '1', '%' ); ?></a></span>
                                                        </li>
                                                    </ul>

                                      <div class="clearfloat"></div><!-- Very Important -->

                        </li>

                <?php
                        //increment featured count offset variable
                        $count_featured_offset++;

                    endwhile; 
                    endif;
                ?>

                <?php wp_reset_query(); ?>




                <?php
                    //here we calculate correct offset depending on number of featured listings 2 1 0
                    $listing_per_page = $listing_per_page - $count_featured_offset;

                    //count to insert ads after nth post, 5th place in this example
                    $adspostnum = 1;
                    $adsshowpos = 1;

                    //normal listing loop
                    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
                    query_posts( array( 'order' => $listing_order_perpage, 'orderby' => $listing_order_logic_perpage, 'posts_per_page' => $listing_per_page, 'post_type' => 'post', 'cat' => $current_category_id, 'post__not_in' => $do_not_duplicate, 'paged' => $paged ) );
                    if ( have_posts() ) : while ( have_posts() ) : the_post();
                ?>

                                    <li>
                                             <a href="<?php the_permalink(); ?>">
                                                <?php 
                                                    if ( has_post_thumbnail() ) {
                                                        echo get_the_post_thumbnail($post->ID, 'thumbnail', array('alt' => the_title('','',false), 'title' => get_the_excerpt(), 'class' => 'primarythumb' ));
                                                    } else {
                                                        echo '<img width="120" height="68" class="primarythumb" alt="'.the_title("","",false).'" title="'.get_the_excerpt().'" src="'.get_bloginfo('template_url').'/thumbs/post-thumb-na.png">';
                                                } ?>
                                             </a>

                                          <h3><a class="title" href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>

                                        <?php
                                          echo "<p>".__(get_post_meta($post->ID, 'Address', true))."</p>";
                                          echo '<span class="telephonebtn">'.get_post_meta($post->ID, 'Phone', true).'</span>';

                                            $link = get_post_meta($post->ID, 'url', true);

                                            if ( $link ) {
                                              echo '<span class="wwwbtn wwwon"><a href="'.$link.'" target="_blank">'.__('Official Website','holidayge').'</a></span>';
                                            } else {
                                              echo '<span class="wwwbtn wwwoff"><a href="#">'.__('Official Website','holidayge').'</a></span>';
                                            }
                                        ?>

                                          <p class="infoslidedescrpt"><?php echo trim(strip_tags($post->post_excerpt)); ?></p>


                                                        <ul class="reviewsdetail">
                                                            <li class="reviewsrightborder">
                                                                <?php echo ( wp_gdsr_rating_article()->votes ); ?> <?php _e('users rated','holidayge'); ?>
                                                                <div><?php if(function_exists('wp_gdsr_render_article')){ wp_gdsr_render_article();}?></div>
                                                            </li>
                                                            <li class="reviewsleftborder">
                                                                <?php _e('Number of reviews','holidayge'); ?>
                                                                <span><a href="<?php echo get_permalink();?>#comments"><?php comments_number( '0', '1', '%' ); ?></a></span>
                                                            </li>
                                                        </ul>

                                          <div class="clearfloat"></div><!-- Very Important -->
                                    </li>

                <?php
                    //in betweeen listing advertising goes here
                    if ( $adspostnum == $adsshowpos ) {
                    ?>
                        <li>

<div id="sideleftads">

    <?php //echo adrotate_block(2); ?>

    <a href="<?php echo get_permalink( 11 ); ?>"><?php echo get_the_title( 11 ); ?></a>

<!-- End #sideleftads --></div>

                        </li>
                    <?php } $adspostnum++;



                    endwhile; 
                    endif;
                ?>

        <!-- End .infoslide --></ul>








        <div class="catlistpagination">
           <?php wp_pagenavi(); ?>
        </div>
share|improve this question
1  
a simple way to make your pagination issues go away is to never use query_posts in the template. create additional queries with WP_Query. Modify the main query via the pre_get_posts action. –  Milo Oct 8 '12 at 16:09
    
@Milo as an answer! :) –  Rarst Oct 10 '12 at 1:01
    
@Milo found faster solution, but thanks for your time. –  Sandro Dzneladze Oct 10 '12 at 12:16
add comment

2 Answers

While comment is absolutely correct about what you should be doing, I want to elaborate a bit why exactly query_posts() is recipe for horribly broken pagination.

Main WordPress query runs before execution reaches template file, which is actually chosen depending on results of it. When query runs for archives it takes global settings of post per page and figures out which specific posts belong on current page.

Problem is - when you are trying to tweak quantity of posts on page in template it becomes completely out of sync with main query. So while your in-template query might think that there should be something on page three... Main query is oblivious and can easily conclude that there aren't enough posts for it. Thus 404 and execution never reaching template file.

In a nutshell - never modify main query in template file. Especially when dealing with pagination. But "never" is more reliable. :)

share|improve this answer
    
found faster solution to the problem, and also understood why it happens. ty for your time. –  Sandro Dzneladze Oct 10 '12 at 12:17
add comment

I discovered a simpler solution. Simply setting number of posts displayed less than minimum on category pages fixes this error.

I found very good explanation of this problem here: http://wordpress.org/support/topic/explanation-and-workaround-for-error-404-on-category-pagination

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.
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.