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.

OK, seriously. I have a really modified index.php. The result: after the second paginated page, the page returns Not found and 404, while the posts are shown already.

Can anybody tell me, what it happening?

Working example with the problem:

and so on.

The codes:

index.php

<?php
    define ('REDIRECT_TO_PORTFOLIO', get_option ('siteurl') . '/portfolio/through-a-lupe/' );
    $_query_string = $query_string;

    if ( is_home() || is_front_page() )
    {
        wp_redirect( REDIRECT_TO_PORTFOLIO );
        exit;
    }

    $cat = get_query_var('cat');
    if ( !empty( $cat ) ) :
        $show_pagination = false;
        $show_sidebar = false;
        $posts_per_page = get_option('posts_per_page ');
        $category = get_category( $cat );
        $orderby = 'date';
        $order = 'DESC';
        switch ($category->slug) :
            case 'portfolio':
                    wp_redirect( REDIRECT_TO_PORTFOLIO );
                    exit;
                break;
            case 'photoblog':
                $posts_per_page = 4;
                $show_pagination = true;
                break;
            case 'wordpress':
                $orderby = 'modified';
                $show_sidebar = true;
                break;
            default:
                $show_sidebar = true;
                break;
        endswitch;
        $_query_string = $query_string . '&posts_per_page=' . $posts_per_page . '&order=' . $order . '&orderby=' . $orderby;
    endif;

    get_header();
    query_posts( $_query_string );

    if ( $show_sidebar ) :
    ?>
        <section class="category-postlist">
    <?php
    endif;

    if ( have_posts() ) :
        while ( have_posts() ) :
            get_template_part('singles');
        endwhile;
    endif;

    if ( $show_sidebar ) :
    ?>
        </section>
        <section class="sidebar">
    <?php
        echo wp_list_posts( -1, get_option('posts_per_page ') );
    ?>
        </section>
    <?php
    endif;

    if( function_exists('wp_paginate') && $show_pagination )
        wp_paginate();

    get_footer();
?>

SOLVED

I could solved after a few hour, please see the first comment for explanation. It is caused by some kind of bug in pagination counter, it always uses the default value to count the pagination instead of the new one. Pagination would have to be re-calculated after a query_posts with per post values.

share|improve this question

2 Answers 2

up vote 0 down vote accepted

It's not correct solving of issue and it's not a bug. Here is explanation why it happens: "Pagination is calculated before you get to the template file that runs query_posts. The proper way to alter posts_per_page conditionally is to use the pre_get_posts hook to modify the main query." http://wordpress.stackexchange.com/a/42063/17177 I found one implementation "pre_get_posts" that works for me http://uncommoncontent.com/2012/01/28/add-custom-post-types-to-the-loop:

if ( ! function_exists( 'ucc_add_cpts_to_pre_get_posts' ) ) {
    function ucc_add_cpts_to_pre_get_posts( $query ) {
        if ( $query->is_main_query() && ! is_post_type_archive() && ! is_singular() && ! is_404() ) {
        $my_post_type = get_query_var( 'post_type' );
            if ( empty( $my_post_type ) ) {
                $args = array(
                    'exclude_from_search' => false,
                    'public' => true,
                    '_builtin' => false
                );
                $output = 'names';
                $operator = 'and';
                $post_types = get_post_types( $args, $output, $operator );

                // Or uncomment and edit to explicitly state which post types you want.
                // $post_types = array( 'event', 'location' );
                // Add 'link' and/or 'page' to array() if you want these included.
                // array( 'post', 'link', 'page' ), etc.
                $post_types = array_merge( $post_types, array( 'post' ) );
                $query->set('post_type', $post_types );
            }
        }
    } 
}

add_action( 'pre_get_posts', 'ucc_add_cpts_to_pre_get_posts' );
share|improve this answer
    
Thanks for the comment, this is clearly better. –  petermolnar Jun 17 '12 at 7:47

OK, this is the most stupid bug I've ever seen.

According to this solution by Iriswebsolutions, the problem rises when the default per page is larger than the one in the customized query.

Solution: change the default to 1, add $posts_per_page = 10; to the default part of my switch-case section.

EDIT

So the code will be (just the modified parts):

    if ( !empty( $cat ) ) :
        $show_pagination = false;
        $show_sidebar = false;

        /* this would get the default post per page, but it's buggy */
        /* $posts_per_page = get_option('posts_per_page '); */

        /* the true default post per page you want */
        $posts_per_page = 10; 

The rest is the same.

share|improve this answer
    
Did you find a working solution then?? –  Rutwick Gangurde Dec 29 '11 at 9:30
    
Yes. Lower the default posts per page to 1, add your own default value in index.php. I added the modified code to the answer. –  petermolnar Dec 30 '11 at 12:29
    
Cool... Please accept this answer then! –  Rutwick Gangurde Dec 30 '11 at 16:55

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.