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 ) );

If the file in question is a custom page template, replace it with this:

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

Otherwise, replace it with this:

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

Note that pagination might not work correctly if you use query_posts() in other places besides page templates.

Also see the Codex page on query_posts().

Comments (1)