WordPress “Query_Posts” Page Navigation Problem Fixed/Solved

If you use the function “Query_Posts” to display your posts, you may notice that page navigation does not work. When you click on “Older Posts” to go to “page 2”, (as in www.example.com/page/2/) you see the same posts as you did on “page 1”, and the same happens for every page thereafter. Essentially, you only see the first “batch” of posts, regardless of what page you are on.

The fix for this appeared in many postings, but did NOT actually work for me because it only applies to WordPress versions prior to version 3.0.2.

If you are running WordPress version 3.0.2 or higher, add the following line right before the “Query_Posts” function:

<php $paged = (get_query_var('page')) ? get_query_var('page') : 1; ?>

So that at the minimum, you would have this:

    $paged = (get_query_var('page')) ? get_query_var('page') : 1;
    query_posts('cat=1&posts_per_page='.get_option('posts_per_page').'&paged=' . $paged);
    get_template_part( 'loop' );

If you are running an earlier version of WordPress the line to add is as follows:

<?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; ?>

Did you catch the difference? In version 3.0.2 and higher it’s get_query_var(‘page’), and in earlier versions it’s get_query_var(‘paged’), i.e. (‘page’) vs. (‘paged’).

This information can be found in the WordPress Codex – Class Reference/WP Query which also states:

Pagination Note: You should set get_query_var( ‘page’ ); if you want your query to work with pagination. Since WordPress 3.0.2, you do get_query_var( ‘page’ ) instead of get_query_var( ‘paged’ ). The pagination parameter ‘paged’ for WP_Query() remains the same.

Sounds like we’re done, but noooooo. It gets a little crazier first. Here is what I discovered:

The above is true when I use “query_posts” in a static front page. This is when ‘page’ works and ‘paged’ does NOT. HOWEVER, in all other pages it is the opposite. I’ve had to use ‘paged’ to get pagination to work as ‘page’ did NOT. Are we having fun yet?

I hope it didn’t take you as long to figure this out as it did me. If this helped you, I’d love to hear about it.. šŸ™‚

5 thoughts on “WordPress “Query_Posts” Page Navigation Problem Fixed/Solved”

  1. hello there,

    need help I am looking for a wp plugin for student search results on front end. like they usually search for 12th std results after login and enter seat no, or DOB and hit enter to search results is there any such in wordpress or can do this by plugin..

    also the student DB file will be uploaded from admin..the fields might be name, seat no, etc….

    when user enter his seat no then according to year his results should display…and so on

    urgent require some help here..hope you can help..

    thank you

Leave a Reply