WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Page navigation doesn't show when query category (6 posts)

  1. cikatomo
    Member
    Posted 1 year ago #

    **Problem**:
    when I do http://www.example.com?cat=4 or choose category through custom menu or from category widget it shows only first page without navigation at the bottom, where it supposed to be. It's the same if I use index.php or category-slug.php as template. For test, when I do http://www.example.com?year=2012 it works. Also, http://www.example.com?cat=4&paged=2 works. So it must be something with categories.

    **My setup:**
    Wordpress 3.5, Free WP tube theme, SQLite

    **Relevant code:**
    index.php

    <?php get_header(); ?>
    
        	<!-- code to get theme options and setup $orderby -->
        							query_posts($query_string.$orderby.'&cat=4'); //in this case showing category with navigation works
                                    if (have_posts()) : ?>
                                    <?php $i=0; while (have_posts()) : the_post(); $i++; ?>
    
        				<!-- code for displaying posts -->			
    
                                    <?php if($i%3==0) : ?><div class="clear"></div><?php endif; ?>
    
        							<?php endwhile; wp_reset_query(); ?>
                                    <?php
                                    $next_page = get_next_posts_link('Previous');
                                    $prev_pages = get_previous_posts_link('Next');
                                    if(!empty($next_page) || !empty($prev_pages)) :
                                    ?>
                                    <!-- navigation -->
                                    <div class="navigation">
                                        <?php if(!function_exists('wp_pagenavi')) : ?>
                                        <div class="alignleft"><?php echo $next_page; ?></div>
                                        <div class="alignright"><?php echo $prev_pages; ?></div>
                                        <?php else : wp_pagenavi(); endif; ?>
                                    </div>
                                    <!-- /navigation -->
                                    <?php endif; ?>

    Note: you can see I'm using the &cat=4 in query. It is the same as category_name=video because I want to show only videos on home page. But also, I want to make able to choose that category from custom menu in which case doesn't show page navigation.

    **What I have tried:**

    Making $wp_query = null then making new query before the Loop.
    http://wordpress.org/support/topic/next-page-navigation-doesnt-work.
    Basically any variation of this

    <?php
        $myqueryname = $wp_query;
        $wp_query = null;
        $wp_query = new WP_Query();
        $wp_query->query('showposts=5'.'&paged='.$paged);
        ?>

    Doesn't work.

    Adding $paged as suggested by the creator of WP-pagenavi and forums:

    <?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; ?>
                                          <?php query_posts("cat=-11");; ?>

    Doesn't work.

    **Testings:**
    besides the simple ones in the "problem" part of the question i did print_r($wp_query) all the way to see where to problem starts and it starts right on the beginning, ie. when i put print_r($wp_query) as the first line in header.php. So, the problem is not in the custom query of the theme, am I right?
    I did it for the working $wp_query and not working $wp_query then compared them.
    This is the relevant comparison part of the code:
    Working $wp_query:

    [category_name] =>
        [cat] => 4
        ...
        <strong>**//most important part**</strong>
        [found_posts] => 382
        [max_num_pages] => 43
        ...
        [request] => SELECT SQL_CALC_FOUND_ROWS  wp_posts.ID FROM wp_posts  WHERE 1=1  AND wp_posts.post_type = 'post' AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'private')  ORDER BY wp_posts.post_date DESC LIMIT 0, 9
        ...
        //here shows code for some posts even if not displaying them, because they're not in the cat=4 or category_name=video

    non working $wp_query:

    [category_name] => video
        [cat] => 4
        ...
       <strong> **//most important part**</strong>
        [found_posts] => 1
        [max_num_pages] => 1
        ...
        [tax_query] => WP_Tax_Query Object
                (
                    [queries] => Array
                        (
                            [0] => Array
                                (
                                    [taxonomy] => category
                                    [terms] => Array
                                        (
                                            [0] => video
                                        )
    
                                    [include_children] => 1
                                    [field] => slug
                                    [operator] => IN
                                )
    
                        )
    
                    [relation] => AND
                )
        ...
        [query] => Array
                (
                    [category_name] => video
                )
    
            [request] => SELECT SQL_CALC_FOUND_ROWS  wp_posts.ID FROM wp_posts  INNER JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id) WHERE 1=1  AND ( wp_term_relationships.term_taxonomy_id IN (4) ) AND wp_posts.post_type = 'post' AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'private') GROUP BY wp_posts.ID ORDER BY wp_posts.post_date DESC LIMIT 0, 9
        ...
        //this $wp_query doesn't show code for the posts which are not from video category
        ...
        [queried_object] => objArray Object
                (
                    [term_id] => 4
                    [name] => Video
                    [slug] => video
                    [term_group] => 0
                    [term_taxonomy_id] => 4
                    [taxonomy] => category
                    [description] =>
                    [parent] => 0
                    [count] => 379
                    [cat_ID] => 4
                    [category_count] => 379
                    [category_description] =>
                    [cat_name] => Video
                    [category_nicename] => video
                    [category_parent] => 0
                )

    Also, from the index.php code get_next_posts_link('Previous') returns NULL.

    **Conclusion:**

    something is happening before the template files with default query, but I don't know what. Can somebody help me to show page navigation?

    P.S. formatting doesn't work, look here for the more clean version http://wordpress.stackexchange.com/questions/82313/page-navigation-doesnt-show-when-query-category
    Thank for helping!

  2. cikatomo
    Member
    Posted 1 year ago #

    0 down vote accept

    I found a workaround. Since the query object in the index.php is the right one I transferred it using serialize to template of choice, in this case category-video.php.

    In index.php i put on top

    <?php $s = serialize($wp_query);
    file_put_contents('query',$s); ?>

    and in category-video.php I put

    <?php $u = file_get_contents('query');
    $wp_the_query = unserialize($u); ?>

    It's not the solution, but It's workaround and the page navigation is working now.

    Is this some kind of bug?

  3. cikatomo
    Member
    Posted 1 year ago #

    Look at my whole answer here:
    http://wordpress.stackexchange.com/a/82350/26237

  4. keesiemeijer
    moderator
    Posted 1 year ago #

    Try it with "pre_get_posts":
    http://codex.wordpress.org/Pagination#Removing_query_posts_from_the_main_loop

    Or try it by adding the paged variable to the query:

    <?php
        $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
        $myqueryname = $wp_query;
        $wp_query = null;
        $wp_query = new WP_Query();
        $wp_query->query('showposts=5'.'&paged='.$paged);
    ?>
    
    // or in your other example
    
    <?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; ?>
    <?php query_posts("cat=-11&paged=".$paged); ?>

    http://codex.wordpress.org/Pagination#Adding_the_.22paged.22_parameter_to_a_query

    If you use the wp-pagenavi plugin try passing the query to it:
    http://scribu.net/wordpress/wp-pagenavi/wpn-2-74.html

  5. cikatomo
    Member
    Posted 1 year ago #

    I will try with pre_get_post, but other solution is not working.
    You see, problem is not in the page, it is because $wp_query->found_posts always return 1

  6. cikatomo
    Member
    Posted 1 year ago #

    This is caused by PDO for wordpress plugin and Sqlite. Something is broken. I don't know what, I'm switching to myaql.

Topic Closed

This topic has been closed to new replies.

About this Topic