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;
		'post_type' => 'portfolio',
		'orderby' => 'date',
		'order' => 'DESC',
		'posts_per_page' => 10

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.

// The Query
global $wp_query;
 		'post_type' => 'portfolio',
		'orderby' => 'date',
		'order' => 'DESC',
		'posts_per_page' => 10

// The Loop
if (have_posts()) :
   while (have_posts()) :

       // Display Content


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!!!

Enjoyed this post?

25 Comments (Post your comment)

  • Trackback by — February 10, 2011 @ 5:28 am

    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….

  • Comment by Chris — March 27, 2011 @ 9:38 am

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

  • Comment by Xcellence-IT — April 4, 2011 @ 10:10 am

    Glad to know this. Thank you.

  • Comment by daniel mamann — May 29, 2011 @ 3:08 am

    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

  • Comment by Xcellence-IT — May 30, 2011 @ 7:32 am

    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.

  • Comment by Siniša — August 4, 2011 @ 8:37 am

    Nice solution, will try ASAP.

  • Comment by James — August 13, 2011 @ 6:16 pm

    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.


  • Comment by Xcellence-IT — August 14, 2011 @ 1:12 pm

    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.

  • Comment by jillian madison — September 7, 2011 @ 6:35 pm

    I’m having the exact same problem, with PAGE 2 returning a “page not found” error. Any idea why this is happening?

  • Comment by Xcellence-IT — September 9, 2011 @ 1:06 pm

    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.

  • Comment by Dan Excell — September 13, 2011 @ 10:46 pm

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

  • Comment by Dan Excell — September 13, 2011 @ 10:48 pm

    Sorry, added php tags;

    query_posts(‘post_type=page&orderby=menu_order&order=asc&post_parent=144&posts_per_page= 2′ );

    if (have_posts()) : while (have_posts()) : the_post();

  • Comment by Xcellence-IT — September 14, 2011 @ 4:33 am

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

  • Comment by Hazel — October 26, 2011 @ 11:53 pm

    Good to know! Thank you!

  • Comment by Essaem — January 26, 2012 @ 3:42 pm

    Life saver !

  • Comment by Jill — February 15, 2012 @ 2:19 pm

    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.

  • Comment by carina — April 11, 2012 @ 4:38 pm

    Still not working. That problem is killing me.

  • Comment by Asker of Relevant Questions — May 22, 2012 @ 10:13 pm

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

  • Comment by Asker of Relevant Questions — May 22, 2012 @ 10:27 pm
  • Comment by Eric — May 23, 2012 @ 4:01 am

    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?

  • Comment by James Cox — August 4, 2012 @ 12:00 am

    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 :


  • Comment by Xcellence-IT — August 7, 2012 @ 1:57 pm

    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?

  • Comment by bruno — September 9, 2012 @ 11:18 pm

    Hi everybody,

    I want that in the archive page ( and in widget archives shows 20 posts per page..

    How can I get this???

    Please help me, I appreciate a lot

  • Comment by Jonathan — April 26, 2014 @ 3:08 am

    Really thanks, it works for me. =)

  • Comment by Xcellence-IT — April 26, 2014 @ 5:06 am

    Jonathan, Glad it helped you!

RSS feed for comments on this post. TrackBack URL

Leave a comment

Allowed tags:
You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Please do not spam, no link dropping, no keywords or domains as names; and do not advertise!

Live Chat or Call us at +91-99099 55948     Client Login