Fix Wordpress Pagination Problems when used with query_posts

Query_posts overrides nearly everything in standard WP Loop query, and hence, Pagination stops working on pages when query is customized with query_posts. Here is a simple fix to fix pagination issue when used with query_posts.

Fix WordPress Pagination Problems when used with query_posts

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.

We love to hear your comments!!!

Related Posts

25 COMMENTS

  1. Chris
    March 27, 2011 09:38

    this solved my problem. thanks for sharing this resource buddy.

    • April 04, 2011 10:10

      Glad to know this. Thank you.

  2. May 29, 2011 03:08

    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

    • May 30, 2011 07:32

      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.

  3. August 04, 2011 08:37

    Nice solution, will try ASAP.

  4. James
    August 13, 2011 18:16

    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

    • August 14, 2011 13:12

      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.

  5. jillian madison
    September 07, 2011 18:35

    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/

    • September 09, 2011 13:06

      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.

  6. September 13, 2011 22:46

    I tried to code above and I got the error page instead. This is the code that I am using

  7. September 13, 2011 22:48

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

    • September 14, 2011 04:33

      Are you using some ready made themes, or developing your own? Are you using any plugin for pagination?

  8. Hazel
    October 26, 2011 23:53

    Good to know! Thank you!

  9. January 26, 2012 15:42

    Life saver !

  10. Jill
    February 15, 2012 14:19

    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.

  11. carina
    April 11, 2012 16:38

    Still not working. That problem is killing me.

  12. Asker of Relevant Questions
    May 22, 2012 22:13

    I’m not able to get this to work either. Has anyone successfully tested this with WP 3.3?

  13. Asker of Relevant Questions
    May 22, 2012 22:27
  14. Eric
    May 23, 2012 04:01

    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?

  15. James Cox
    August 04, 2012 00:00

    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!

    • August 07, 2012 13:57

      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?

  16. bruno
    September 09, 2012 23:18

    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

  17. Jonathan
    April 26, 2014 03:08

    Really thanks, it works for me. =)

    • April 26, 2014 05:06

      Jonathan, Glad it helped you!