Pagination breaks on child-categories, works fine on parent-category

I have two parent categories, and pagination is working fine on them - the paged url's show as:

/parent-category-1/page/2/ ... etc
/parent-page/parent-category-2/page/2/ ... etc

However, when I go to a child-category, pagination returns a 404 - for e.g.:

/category/parent-category-1/child-category/page/2/ ... etc = 404
/category/parent-category-2/child-category/page/2/ ... etc = 404

  • I'm using the WP-PageNavi plugin for my pagination.
  • I don't have any custom permalinks set in my permalinks options.

parent-category-1 parents the categories for my standard blog posts.
parent-category-2 parents the categories for a custom post type.

Here is the template for the blog posts under parent-category-1 (standard template - category.php):

div class="feed med"
    ?php   

     $paged = (get_query_var('page')) ? get_query_var('page') : 1; //  FOR PAGINATION

        $args = array(
           'post_type' = 'post',
           'category__in' = ($cat),
           'posts_per_page' = 5, 
           'page' = $paged, //  FOR PAGINATION
        );

        // FOR PAGINATION - hijack the $wp_query variable temporarily
        $temp = $wp_query;
        $wp_query= null;
        $wp_query = new WP_Query($args);

        $second_query = new WP_Query( $args );
        if ( $second_query-have_posts() ):
           while( $second_query-have_posts() ) : $second_query-the_post();

           $titlechars = 45000; // Character Limit - rediculously large number to eliminate limit
           $posttitle = get_the_title();
           $modtitle = substr($posttitle, 0, $titlechars);

           $contentchars = 120; // Character Limit
           $postcontent = get_the_excerpt();
           $modcontent = substr($postcontent, 0, $contentchars);

        echo 'article';
    ?

    ?php
        if( get_field('image') ):

            $attachment_id = get_field('image');
            $size = 'customfeatins'; // (thumbnail, medium, large, full or custom size)
            $image = wp_get_attachment_image_src( $attachment_id, $size );

        echo 'a href="' . get_permalink() . '"img src="' . $image[0] . '" alt="' . get_the_title() .'" width="136" height="90" //a';
    ?

    ?php else : ?

        ?php echo 'a href="' . get_permalink() . '"img src="'. get_template_directory_uri() .'/assets/img/content/bf-default.gif" alt="bf-default" width="136" height="90" //a' ?

    ?php endif; ?

    ?php
        echo '
            div class="right-content"
            h3 class="purple"a href="' . get_permalink() . '"' . $modtitle .'/a/h3
            p class="date"' . get_the_date() .'/p
            p' . $modcontent . 'hellip; a href="' . get_permalink() . '"More rsaquo;/a/p
            /div

            /article';
    ?

    ?php   

        /* PageNavi at Bottom */
        if (function_exists('wp_pagenavi')){wp_pagenavi();} 

        // FOR PAGINATION -  reassign the $wp_query variable to what is was originally and then reset the query back to start.
        $wp_query = null;
        $wp_query = $temp;  

        endwhile;
    ?


    ?php
        endif;
        wp_reset_postdata(); // to reset the loop
    ?
/div!-- [END] feed--

And the template for the custom-posts under parent-category-2 (a custom template - category-parent-category-2.php):

div class="feed med no-border" id="the-posts"

    ?php   

     $paged = (get_query_var('page')) ? get_query_var('page') : 1; //  FOR PAGINATION

        $category = get_the_category();             
            $args = array(
               'post_type' = 'custom-post-type',
               'posts_per_page' = 5, 
               'page' = $paged, //  FOR PAGINATION
               'category__in' = ($cat),
            );

        // FOR PAGINATION - hijack the $wp_query variable temporarily
        $temp = $wp_query;
        $wp_query= null;
        $wp_query = new WP_Query($args);        

        $second_query = new WP_Query( $args );
        if ( $second_query-have_posts() ):
           while( $second_query-have_posts() ) : $second_query-the_post();

           $titlechars = 45; // Character Limit
           $posttitle = get_the_title();
           $modtitle = substr($posttitle, 0, $titlechars);

           $contentchars = 120; // Character Limit
           $postcontent = get_the_excerpt();
           $modcontent = substr($postcontent, 0, $contentchars);

        echo 'article ';
        echo ' ' . post_class('',false) . '  ';
        echo '';
    ?

    ?php

        if( get_field('image') ):

            $attachment_id = get_field('image');
            $size = 'customfeatins'; // (thumbnail, medium, large, full or custom size)
            $image = wp_get_attachment_image_src( $attachment_id, $size );

        echo 'a href="' . get_permalink() . '"img src="' . $image[0] . '" alt="' . get_the_title() .'" width="136" height="90" //a';
    ?

    ?php else : ?

        ?php echo 'a href="' . get_permalink() . '"img src="'. get_template_directory_uri() .'/assets/img/content/bf-default.gif" alt="bf-default" width="136" height="90" //a' ?

    ?php endif; ?

    ?php
        echo '
            div class="right-content"
            h3a class="purple" href="' . get_permalink() . '"' . $modtitle .'/a/h3
            p class="date"' . get_the_date() .'/p
            p' . $modcontent . 'hellip; a href="' . get_permalink() . '"More rsaquo;/a/p
            /div

            /article';
    ?

    ?php   

        /* PageNavi at Bottom */
        if (function_exists('wp_pagenavi')){wp_pagenavi();} 

        // FOR PAGINATION -  reassign the $wp_query variable to what is was originally and then reset the query back to start.
        $wp_query = null;
        $wp_query = $temp;  
    ?

    ?php                              
        endwhile;
        endif;
        wp_reset_postdata(); // to reset the loop
    ?

/div!-- [END] feed --

I've searched through questions posted here and through the wp forums - I haven't found anything that deals with child-categories specifically.

Any help here would be much appreciated!!!

Thank you in advance.

Topic plugin-wp-pagenavi permalinks categories pagination 404-error Wordpress

Category Web


I was finally able to get this working! Here's how:

What I had misunderstood (being a WP noob and all...) is that get_query_var('page') is for pages, and get_query_var('paged') is for posts. http://codex.wordpress.org/Function_Reference/get_query_varfor

So I replaced these two lines

$paged = (get_query_var('page')) ? get_query_var('page') : 1; // FOR PAGINATION 'page' => $paged, // FOR PAGINATION

with these two lines

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1; // FOR PAGINATION 'paged' => $paged, // FOR PAGINATION

and removed the 'posts_per_page' => 5, parameter from my templates, then set the post limit in the admin under Settings > Reading

This wasn't the final solution, however. Pagination was still returning a 404 on custom_post_type categories. The final fix was adding this patch: http://wordpress.org/support/topic/custom-types-category-pagination-404#post-1913902 to my functions.php file.

Now pagination works perfectly on categories, and child-categories, on all post-types.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.