While working with WordPress, most of WP developers face problems with pagination especially when loop is customized with query_posts(). Query Posts is a powerful function provided by WordPress which offers numbers of options to control which posts are show up in Loop.
I have gone through several sites where various fixes are available to fix Pagination issues, but only few of them worked when query_posts has been used. Moreover, most of them are related to modifying WP loop, which is not the proper way to fix this simple issue.
Why do you need to modify a WP loop if you can customize post results using query_posts()? By just using query_posts() you’ll save yourself from editing being made in Loop. You just need to use query_post on your template file and it will be customized as per your requirement.
For more details how to use query_posts(), please refer to WordPress Codex on query_posts().
Coming back to Pagination Problem when used with query_posts()
Many of the developers face problems with Pagination while using query_posts(). Developers who are new to WordPress doesn’t know how query_posts works, and has no clue, why Pagination stops working.
Why Pagination fails when used with query_posts:
Here is the reason, why Pagination stops working with query_posts:
The query_posts() function overrides and replaces the main query for the page. The query_posts() function creates a new WP_Query object and assign it to global wp_query variable. The wp_query object will generate a new SQL query using your parameters. When you do this, WordPress ignores the other parameters it receives via the URL (such as page number or category).
This not only cause Pagination to fails, but also other parameters to fail, like category/tag, or any other parameters which it receives via the URL.
How to fix Pagination Problem:
By default running query_posts will completely overwrite all existing query variables on the current page. Pagination, categories, dates etc. will be lost and only the variables you pass into query_posts will be used.
Doing this will merge the original query array into parameter array which preserves original query including pagination, category, etc. You can pass your query_post parameter in an array just like I passed custom post type, orderby, order and posts per page in the following example.
global $wp_query; query_posts( array_merge( array( 'post_type' => 'portfolio', 'orderby' => 'date', 'order' => 'DESC', 'posts_per_page' => 10 ), $wp_query->query ) );
This fix will work with most Pagination plugin too. I’ve tested it for WPPageNavi, one of great Pagination Plugin for WordPress. You need to put this code before the loop starts. Just like, following example.
<?php // The Query global $wp_query; query_posts( array_merge( array( 'post_type' => 'portfolio', 'orderby' => 'date', 'order' => 'DESC', 'posts_per_page' => 10 ), $wp_query->query ) ); // The Loop if (have_posts()) : while (have_posts()) : the_post(); // Display Content the_content(); endwhile; endif; ?>
Note that depending on the theme you’re using you might have different Loop. Make sure you insert above code before Loop starts. For more information on WP Loop, refer to the WordPress Codes on Loop.
This will fix Pagination problems/issue when you’re using query_posts on your template. I will look forward to you for your comments/questions.
I found few related articles which is worth considering if your problem is not fixed by above fix.
- How to create a wordpress query_post with pagination
- query_posts() Pagination problem
- WordPress Tip-Fixing Pagination on Custom Queries With query_posts
- WordPress Pagination With WP-PageNavi & query_posts()
- Custom WordPress Loop Breaks Pagination
- How to fix broken pagination when using query_posts