StylizedWeb

Subscribe to our updates:

A Design, WordPress and Tutorials Blog.

Dedicated to helping you learn the art and science of the web.

query_posts() Pagination problem

Ok, this has bothered me for a long time and now i have finally found a solution for it thanks to Kafkaesqui moderator on WordPress Support. As the title says when you are using a query_posts() to exclude, include categories or what ever you want pagination dosen't work it shows same posts on every page (This is for all pages that use query_posts not just index, so it's for templates etc...)

Where's the problem? query_posts() is a powerful function, but in this situation it has a flaw: it overrides nearly everything in the standard posts object query, including what the paged offset is.

How to fix it? To get proper pagination with query_posts() we need to recreate it through the 'paged' parameter or query. Best way to do this is to ask WordPress for the "page" we happen to be on, and use that as our 'paged' value. There's the code for it

  1.  
  2. <?php if (have_posts()) : ?>
  3. <?php query_posts("category_name=somecat"); ?>
  4. <?php while (have_posts()) : the_post(); ?>
  5.  
  6. replace with
  7.  
  8. <?php if (have_posts()) : ?>
  9. <?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; query_posts("category_name=somecat&paged=$paged"); ?>
  10. <?php while (have_posts()) : the_post(); ?>
  11.  

And that's it! The $paged = line above uses what's called a ternary operator to test whether the 'paged' query variable is available. If it is, $paged receives its value. If it isn't, we assume we're on the first page and assign it '1'. Then we tell query_posts() what page we're on with the addition of &paged=$paged.

Leave a comment on Stylized Web Have some feedback? Leave a comment

198 Comments So Far

  1. By ramesh035 posted on December 24, 2009 at 7:42 am

    thanks a lot gr8 work……….

  2. RAD! Thank you so much.

  3. Man, you saved my day! Awesome.

  4. Thanks!! I’ve been struggling with this for a couple of days!

  5. By Andrew posted on January 7, 2010 at 5:43 pm

    Thanks! I was having trouble because I thought you had to add posts_per_page=$ppp to the query_posts but you do not and it messes it up actually!

    Thank you Thank You!

  6. Thanks! Nice trick :-)

  7. By John Webster posted on February 26, 2010 at 12:31 am

    Thanks for this snippet. Sometimes it’s the little things that get ya.

  8. I was reading the codex for the query_posts function, and it wasn’t very clear on this point. Thanks for posting this!

  9. Thank you, thank you, thank you!

  10. Thanks so much for this!

    I was wondering if there is a way to limit the amount of posts that are viewable?

    Prior to this I was just using So that I could limit the amount of viewable posts to 5. With this code however, I’m not sure where to enter that value. I know you can set the amount of blog posts in the settings, but just wondering if there is a way to do it manually.

    Thank you again!

  11. Pingback: wordpress category issue - DesignersTalk

  12. nice buddy,, thx for this great post!!

  13. hi, Dejan,

    don’t know if anyone said anything about it in previous comments.

    you have a bug in your code, preventing query_posts() to run properly.

    instead of using
    query_posts(“category_name=somecat&paged=$paged”);
    you should use
    query_posts(“category_name=somecat&paged=”.$paged);

    i discovered this while i was trying to list ALL posts and it didn’t work. then i took out the $page variable and concatenated it with the rest of the arguments string and it worked like a charm.

    thanks for the solution though :)

    F

  14. Just what I was looking for, thanks!

  15. By andré posted on April 5, 2010 at 12:25 am

    @ Section09

    Just use “showposts=5″.

    Example:

    Cheers

  16. By andré posted on April 5, 2010 at 12:27 am

    Well that didn’t work.. I’ll just paste the short snippet then.

    query_posts(“cat=category_name&showposts=5&paged=$paged”);

  17. Thanks Bro!

  18. Ok, not this works perfectly. thanks alot!

  19. Thank you for this article. I can’t tell you how helpful this simple snippet has been for solving 90% of the issues I was having with a custom theme build.

    Much appreciated.

  20. By Ian posted on April 15, 2010 at 5:52 pm

    Thanks for the workaround. This had me stumped for a while. I had no idea that query_posts() removes all page variables.

    Just one quick question though. Is it then possible to change the default page links of “Next Page »” and “« Previous Page” to something like “Older posts” and “Newer posts”?

    Thanks

  21. thank you for sharing this valuable information. this will be of great help.

  22. Hey man, thanks heaps for this solution – had exactly this problem with a site. Much appreciated!!

  23. By Stone Deft posted on May 7, 2010 at 4:48 am

    Gee thnx … it didn’t work at first then I realized I have another set of query_post somewhere on my template , resetting that query with wp_reset_query(); did the job.

    Cheers!

  24. This is my ahead opportunity i fall upon here. I found so many provocative baggage in your blog singularly its discussion. From the tons of comments on your articles, I guess I am not the just individual having all the joy here! maintenance up the meet work.

  25. What a great help this is. I was thoroughly stumped. Thank you so much. Also I love your comments section you have going on here.

  26. By k-dawgg posted on May 20, 2010 at 1:36 pm

    you saved me!

  27. Hi,

    This code didnt work for me and I tried multiple variations, I then found a solution which was similar and changed one small bit of your code to get it to work with Pagenavi…..actually I think one of the comments here pointed this out too. You cant us query_posts use $wp_query->query:

    query(‘cat=20&paged=’.$paged); ?>

    I also put another wp_reset_query at the end of loop. I hope this helps some people as its taken me 2 hours to bodge my way through it!!

  28. Sorry code snippet is this:

    query(‘cat=20&paged=’.$paged); ?>

  29. God damn can write the snippet:

    $paged = (get_query_var(‘paged’)) ? get_query_var(‘paged’) : 1; $wp_query->query(‘cat=20&paged=’.$paged);

  30. thanks for the tip, worked well for me

  31. Thanks a lot! great tip

  32. By Caroline posted on June 1, 2010 at 10:26 pm

    Thank you! Works like a charm! I was searching everywhere for a solution, and here it was!

    Cheers :)

  33. Had the same problem and found no solution until i used your code. It works! Thank you!

  34. This worked perfekt for me:

    Thanks a lot!

  35. Nice work! TKS!

  36. This drove me crazy for a while until I came across your post. Thank you sooo much!

  37. Thank you very much ! This is very useful !

  38. Thanks!! You had saved my life!!!

  39. Thanks for this code snippet. Helped a lot !

  40. genius!

  41. This just saved my ass ;) Thanks for putting it together in an easy to follow example!!! You rock.

  42. Thanks … It solved a major problem…. thanks again…

  43. My words can’t express how grateful I am for this bit of code… I’ve been going nuts on the problem for easily 3 hours, not understanding what was wrong. You really saved my day, thanks a lot !

  44. By Alex posted on July 25, 2010 at 7:33 pm

    Thank you so much for writing this up, just what I needed.

  45. Brilliant, thanks.

  46. By luke posted on August 5, 2010 at 9:10 pm

    THANK YOU very much . You saved me…thanks

  47. good job please thank you much

  48. Good Work.

  49. Dude I just wanted to extend a massive THANK YOU for this. I honestly have been googling for an answer to this for at least 6 days – all day – each day!!! lol…..and Im amazed it was as simple as one small string of code!?…Holy crap, figures. Thanks for this :)

  50. seriously, i spent so long trying to deal with this issue. everywhere else i looked and everything else i tried was bunk. your simple line of pretty code restored my faith in humanity and wordpress. cheers!!

Leave a Reply

Your email address will not be published. Required fields are marked *

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