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

 
<?php if (have_posts()) : ?>
    <?php query_posts("category_name=somecat"); ?>
        <?php while (have_posts()) : the_post(); ?>  
 
replace with 
 
<?php if (have_posts()) : ?>
     <?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; query_posts("category_name=somecat&paged=$paged"); ?>
        <?php while (have_posts()) : the_post(); ?>
 

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

173 Comments So Far

  1. Thank you so much for publishing this. It finally worked!!

  2. Pingback: WP: Pagination stays on the same page? * Ivana's Projects

  3. Thank you so much for this solution!

  4. Thank you so much for publishing this. It finally worked!!

  5. By Frank posted on October 20, 2010 at 4:34 pm

    Thanks for your Support, this helps me so much!

  6. Alright peeps…I’ve tried just about everything but can’t seem to find a fix. I’m using the Mystique theme for my church’s site and can’t get pagination to work right. I have the problem described above where “pagination dosen’t work it shows same posts on every page”. Nothing seems to work! Anyone else using this theme that got this to work? Or maybe someone can give me some guidance as to what to do. The site’s address is http://www.vistaassembly.com.

  7. WOW!!!! I take it back…I left out the most important thing “$paged =”. I must’ve been so frustrated that I just overlooked it. THANK YOU SO MUCH FOR PUBLISHING THIS. IT FINALLY WORKED!!! I can sleep now!!!!

  8. Thank you so much for that, it was a real help! :)

  9. Pingback: Problema com paginação e a função query_posts() do Worpress - Dicas e Ajudas WEB

  10. By Jordan posted on November 9, 2010 at 7:21 pm

    I couldn’t get this to work… I had to change the double quotes (“) to single quotes (‘). So instead of this:

    “category_name=somecat&paged=$paged”

    I have this:

    ‘category_name=somecat&paged=$paged’

    Don’t know what the difference is, but that worked for me in case anyone else is having the same issue… thanks for the code!

  11. Thanks for your help with this. I created a custom page on my blog but got stuck when the pagination wouldn’t work. You saved me a lot of time.

    Much appreciated :o )

    @Jordan

    This is just for info really, but you could also use concatenation to complete the query string with the $page variable (whether single or double quotes). Notice the dot:

    query_posts(‘category_name=somecat&paged=’ . $paged)

    Urban Alchemist

  12. You just saved me so much trouble on my blog. I’m a total WordPress newbie, and when someone stumbled across the fact that the second page of my blog listing was the same as my first, I was afraid I’d be spending the entire night trying to fix it. Your post had it fixed for me in a manner of minutes. Great work!

  13. Thank you so much man.
    I was searching for exactly what you present here.

    Seriously, many thanks !!!

  14. cool, but where should I post the change?

  15. By Colin posted on November 24, 2010 at 10:58 pm

    you really saved my ass with this, thanks very much

  16. thanks for the solution

  17. Nice post.. v.v thanks

  18. By Simon posted on December 24, 2010 at 9:24 pm

    Thanks a million for this. I’ve been banging my head against this problem and just lucked on to this page. Great work.

  19. simple and understandable.. tnx!

  20. By Mark Young posted on February 3, 2011 at 5:30 pm

    So would i add this to a template? Here is the problem I am having:

    I am currently finishing up my first WordPress based site for my music
    and am having an issue with the static landing page that I have
    created.

    I am hosting the page locally using XAMPP while I am building the code
    and testing. So here is what is going on:

    1. I have created a page template that is exactly what I need. It
    contains links and images to act as an aggregator.
    2. I then created a matching page that uses that template.
    3. Next I created a “blog” page for my scrolling posts.
    4. After that I revised my READING settings to “A static page”. Then
    selected my front page as the above template, and then my posts page
    to the created “blog” page.

    Everything works amazingly well and as anticipated…except…when you
    click the “blog” tab from any other page, it takes you back to the
    landing page, and not the “blog” page where it should.

    It has to be a simple fix to this loop, and one that I need to solve
    before I publish the page.

    I so appreciate your help everybody

  21. This solves my problem perfectly…thanks!!

  22. Finaly perfectly solution !!! Thanks you a lot !!!

  23. By David posted on February 23, 2011 at 9:00 pm

    Thanks so much! This is exactly the solution I needed.

  24. Thanks heaps, man, that had me stumped for ages… I had a custom page template to show my blog using a catagory…and whamm bamm thank you man !!! all sorted now..cheers !

  25. Pingback: Links to Some Wordpress Related Articles | Learn Web Tutorials

  26. Very useful info. The query_post() pagination problem was a difficult one to solve without this tip. I’m linking to this article as a resource. Thanks.

  27. Just what i needed, this was driving me round the bend. I used:

    cat=-3& instead of category name to exclude a category from the list but include all other cateogories.

    Thanks!

  28. By obarba posted on March 19, 2011 at 4:35 pm

    Perfect!

  29. Awesome! Been looking for this for a few days. Thank you!

    Pro tip: In your params don’t forget the “&paged=$paged” like I did initially :)

  30. If anyone needs to display posts from multiple categories, you need an array in query_posts().

    array(3,4,5), ‘posts_per_page’ => 2, ‘paged’ => $paged ) ); ?>

    This code will display posts from categories 3, 4 and 5.

  31. If anyone needs to display posts from multiple categories, you need an array in query_posts().

    $paged = (get_query_var(‘paged’)) ? get_query_var(‘paged’) : 1; query_posts(array(“category__in” => array(3,4,5), ‘posts_per_page’ => 2, ‘paged’ => $paged ) );

    This code will display posts from categories 3, 4 and 5.

  32. By Rutger posted on April 19, 2011 at 9:58 am

    Many thanks for this solution!

  33. Awesome, thanks for the solution..

  34. Thanks for that one, that did the trick for me !

  35. Works :) Thanks!

  36. By Gavin posted on May 19, 2011 at 11:28 am

    Thanks worked a treat :D

  37. Thank you, This worked perfectly

  38. Hey there! I could have sworn I’ve been to this blog before but after browsing through some of the post I realized it’s new to me. Nonetheless, I’m definitely delighted I found it and I’ll be bookmarking and checking back often!

  39. Hello! I just wanted to ask if you ever have any problems with hackers? My last blog (wordpress) was hacked and I ended up losing many months of hard work due to no back up. Do you have any solutions to stop hackers?

  40. Worked a charm thank you :)

  41. Thanks. heaps, man, that had me stumped for ages… I had a custom page template to show my blog using a catagor

  42. Thank you, This worked perfectly

  43. thank you admin f

  44. By wuerthe posted on July 19, 2011 at 10:59 pm

    thanks :)

  45. Great decision.. thx :)

  46. By lougie posted on July 28, 2011 at 2:19 am

    man! this is a headache ever since to me! i researched into the net for over how many days already and i havent found any answer. You just made it by very simple codes! Thank you so much!^_^

    This is my first time to comment any forum and give thanks., ^_^

  47. Hi

    i was looking for this about 2 hours!!!

    The fuking query_posts() in wordpress codex only have posts_per_page…

    Thx a lot!

  48. By Manolo posted on August 8, 2011 at 4:35 pm

    Thanks!!!

    Three days looking for that… and now it seems so simple!

    Great!

  49. By dsik posted on August 19, 2011 at 8:45 am

    Very helpful, thanks!

  50. i dont have time to read it but I know it is perfect

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>