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
this solved my problem. thanks for sharing this resource buddy.
Glad to know this. Thank you.
Cool blog! Is your theme custom made or did you download it from somewhere? A theme like yours with a few simple tweeks would really make my blog shine. Please let me know where you got your theme. Bless you
Dear Daniel,
We developed design as well as wordpress theme inhouse. We have a in-house team that specializes on Website Design and wordpess development. If you want to design a cool wp theme for your use, please contact us. We’re happy to work with you to make you success.
Nice solution, will try ASAP.
Thanks for the article, but I am using the heatmap theme, and I cant find the loop you are talking about. My next page will only work with default permalinks, not cusotm whih is what I use. Next page gives a 404 error – please help if you can.
Thanks
Hi James,
This requires me to look into the theme you’re talking about. The best option is to ask for support if this is a premium theme. Else, can you share a link to the theme you’re talking about, so I can look into it.
I’m having the exact same problem, with PAGE 2 returning a “page not found” error. Any idea why this is happening?
http://damnyouautocorrect.com/category/submissions/
Hi Jillian, Do you use any ready made theme, or doing a custom integration? Refer to the reference link, they provide deeper insights into this. Awaiting your response.
I tried to code above and I got the error page instead. This is the code that I am using
Sorry, added php tags;
query_posts(‘post_type=page&orderby=menu_order&order=asc&post_parent=144&posts_per_page= 2’ );
//loop
if (have_posts()) : while (have_posts()) : the_post();
Are you using some ready made themes, or developing your own? Are you using any plugin for pagination?
Good to know! Thank you!
Life saver !
Thanks for the code and the clear explanation! This fixed the problem I was having trying to build a custom template that uses Ambrosite Next/Previous Post Link Plus.
Still not working. That problem is killing me.
I’m not able to get this to work either. Has anyone successfully tested this with WP 3.3?
Working answer here:
http://wordpress.org/support/topic/query-posts-and-pagination-links-broken
I was looking through files of my theme and couldn’t find the proper place to add this code. What file am I looking for?
If you can help, I would greatly appreciate it. I use the free heatmap theme in wordpress. My next page & previous page have never worked, and only give 404 errors. They also don’t show up half the time, as they are hidden by the footer. I am going NUTS! Here is a link to mhy site :
http://www.follo-me.com
Thanks!
Dear James, Can you give us exact link where you are facing an error? Moreover, did you checked with the theme developer, does upgrading a newer version solved it?
Hi everybody,
I want that in the archive page (http://trancebrothers.vacau.com/archive/) and in widget archives shows 20 posts per page..
How can I get this???
Please help me, I appreciate a lot
Really thanks, it works for me. =)
Jonathan, Glad it helped you!