WP-PageNavi: The Right Way To Use query_posts()

First rule of using query_posts(): don’t. Use the ‘request’ filter instead.

Ever since I’ve taken over development of the WP-PageNavi plugin, I keep seeing people opening support topics like this:

“PageNavi doesn’t show the correct posts”

“Page numbers don’t work with PageNavi”

etc.

Most of the time, your theme is just using query_posts() wrong.

So here is the correct way to make paging work using query_posts():

Say you have something like this:

query_posts('cat=8');

or like this:

query_posts( array( 'cat' => 8 ) );

You just need to pass along the ‘paged’ query var from the main query:

query_posts( array( 'cat' => 8, 'paged' => get_query_var('paged') ) );

If that doesn’t work, you can also try passing the ‘page’ query var:

query_posts( array( 'cat' => 8, 'paged' => get_query_var('page') ) );

Also see the Codex page on query_posts().

comments powered by Disqus