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.

Problem:
when I do 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 www.example.com?year=2012 it works. Also, 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&paged='.$paged);; ?>

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
...
**//most important part**
[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
...
**//most important part**
[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
        )

Edit #1: This is the comparison BEFORE the custom query! When I dumped $wp_query inside the Loop, after the custom query_posts() they are identical. So the problem is not in the $wp_query. Also, from the index.php code get_next_posts_link('Previous') returns NULL when choose category form menu or ?category_name=video

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?

Edit #2: Since the get_next_posts_link('Previous') returns NULL I knew it's something about the $wp_query->max_num_pages; That number must be bigger than 1 to show navigation. So i tested by putting code <pre><?php echo $wp_query->max_num_pages; ?></pre> to see when it changes from 1 to >1. I discovered that in working version it changes after the <?php endwhile; wp_reset_query(); ?> and in non working it doesn't change.

share|improve this question
    
it's difficult to follow your question, there's a lot of info here to digest and it's honestly a little confusing. the way many themes handle queries and much of the info you'll find on the subject of queries and pagination is just plain wrong- the main query should not be manipulated / modified / overridden in the template. the pre_get_posts action is the correct way to modify any and all main queries. –  Milo Jan 20 '13 at 3:54
    
yes, there's lot of stuff here, but I don't think there's problem in using query_posts cause there's something wrong from the beginning of the default query. Simple ?cat=4 gets just one page in $wp_query->max_num_pages. I don't understand why. –  cikatomo Jan 20 '13 at 5:09
    
in your fist example, what is the value of $query_string and $orderby? in your second example, you pass $paged, but don't set it. in your third example, you set $paged but don't pass it. but again, using query_posts is considered doing it wrong, try via the pre_get_posts action and remove all that query_posts nonsense from your templates. it will be much easier to debug your queries to determine what is happening, and you won't be wasting resources running queries that just get overwritten. –  Milo Jan 20 '13 at 5:21
    
$query_string is empty and $orderby is date. I fixed that with $paged, but it's not so important, I tried every many possible combinations. I just need to get some workaround, for example, to make Page, but with default query just like in index.php then I could easy manipulate it to show me the categories. –  cikatomo Jan 20 '13 at 5:45
    
also, I wanna add for the people reading this, the same thing happens with twenty ten theme which don't have query_posts. –  cikatomo Jan 20 '13 at 6:46
add comment

1 Answer

up vote 0 down vote accepted

I found a partial 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 showing and partially working now. Numbers on navigation don't show current page correctly, but previous and next are working, as well as clicking on page number. It's Wp-PageNavi plugin.

Edit: I fixed page numbers also. For some reason after the wp_reset_query() $wp_query->get('paged') returned wrong number, don't ask me why, I don't know. So I took $paged from before and set new query var after the wp_reset_query.

query_posts($query_string.$orderby);                        
$my_page = $wp_query->get('paged') 

/*code for looping the posts*/

wp_reset_query();
set_query_var('paged',$my_page);

Now, the WP-pagenavi show correct page numbers.

EDIT: the problem is in the PDO plugin and Sqlite, I tested it.

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.