Stay Updated...

Why invest large sums of money on a website when you are going to use photos taken with a $200 camera? 2 days ago@3PointRoss

StylizedWeb is a web design, Wordpress and tutorials blog, maintained by Ross Johnson who also runs a web design company and design blog.

Categories Archive

Popular Posts
Advertisement
Basecamp The Web Design Sketchbook
Najbolje ponuda IT poslova u Srbiji na itposlovi.info Advertise on Stylized Web

Digging Into Wordpress Book

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.

Digging Into Wordpress Book

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



Talk of the town
  • YaWeZ: Thanks Nice Post
  • Shahnawaz: Thank you for sharing css hack.I like your website articles.
  • sikiş: thx for admin..
  • çeviri büroları: An interesting approach to the topic, but I disagree
  • dizi tv izle: To watch the CineMax Movies TV channel you need the SopCast Player. Free Download here. Online TV, Live...
Amigos

What do you think?





100 Responses so far

By ilana
on August 30, 2010

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

By SB
on August 21, 2010

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 :)

By wordpress designer
on August 19, 2010

Good Work.

By sohbet odalari
on August 6, 2010

good job please thank you much

By luke
on August 5, 2010

THANK YOU very much . You saved me…thanks

By Kendra
on August 3, 2010

Brilliant, thanks.

By Alex
on July 25, 2010

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

By ZipionLive
on July 14, 2010

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 !

By Vijay Kimar
on July 13, 2010

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

By Sean Boone
on June 29, 2010

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

By jesse
on June 21, 2010

genius!

By Kadir
on June 19, 2010

Thanks for this code snippet. Helped a lot !

By @shimapi
on June 18, 2010

Thanks!! You had saved my life!!!

By Halcyon
on June 13, 2010

Thank you very much ! This is very useful !

By Clauz
on June 10, 2010

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

By Daniel Ribeiro
on June 10, 2010

Nice work! TKS!

By Olli
on June 9, 2010

This worked perfekt for me:

Thanks a lot!

By Johannes
on June 3, 2010

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

By Caroline
on June 1, 2010

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

Cheers :)

By Juan
on May 30, 2010

Thanks a lot! great tip

By moonpixel
on May 28, 2010

thanks for the tip, worked well for me

By Neil
on May 21, 2010

God damn can write the snippet:

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

By Neil
on May 21, 2010

Sorry code snippet is this:

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

By Neil
on May 21, 2010

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

By k-dawgg
on May 20, 2010

you saved me!

By Benn Wolfe
on May 18, 2010

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.

By Debbie
on May 10, 2010

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.

By Stone Deft
on May 7, 2010

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!

By Coinmaster
on May 4, 2010

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

By private tutor
on April 25, 2010

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

By Ian
on April 15, 2010

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

By Randy
on April 14, 2010

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.

By WPExplorer
on April 13, 2010

Ok, not this works perfectly. thanks alot!

By Alex
on April 9, 2010

Thanks Bro!

By andré
on April 5, 2010

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

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

By andré
on April 5, 2010

@ Section09

Just use “showposts=5″.

Example:

Cheers

By Chris Robinson
on April 1, 2010

Just what I was looking for, thanks!

By flavius
on March 21, 2010

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

By brain
on March 19, 2010

nice buddy,, thx for this great post!!

By Section09
on March 12, 2010

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!

By Dan
on March 9, 2010

Thank you, thank you, thank you!

By Chris Molitor
on March 5, 2010

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

By John Webster
on February 26, 2010

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

By ines
on February 1, 2010

Thanks! Nice trick :-)

By Andrew
on January 7, 2010

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!

By Josh
on January 5, 2010

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

By Victoria
on January 2, 2010

Man, you saved my day! Awesome.

By Jo
on December 28, 2009

RAD! Thank you so much.

By ramesh035
on December 24, 2009

thanks a lot gr8 work……….

By Ryan
on December 5, 2009

Money. And the 50th comment. All I need is a scotch and a blowjob and I’m set.

By Veron
on December 3, 2009

This is exactly the solution I was looking for. I’m working on a WordPress theme and this fixed the problem. Works with WP-PageNavi plugin, too. Thanks!

By Job
on October 30, 2009

Thanks works great!

By Andreas
on October 28, 2009

Just to clarify:

My code goes something like this:
$paged = (get_query_var(’paged’)) ? get_query_var(’paged’) : 1; query_posts(array(’CUSTOM-TAXONOMY’ => ‘CUSTOM-TAX-TERM’));

By Andreas
on October 28, 2009

This is great! Is it possible to integrate it w/ an array instead of the category_name?

My code goes something like this:
$paged = (get_query_var(‘paged’)) ? get_query_var(‘paged’) : 1; query_posts(array(‘anmeldelser’ => ‘anmeldelser’));

how do I integrate that with the pagination?

thanks!

By wpinprogress
on October 17, 2009

It wont allow me to post my code

By wpinprogress
on October 17, 2009

I’m sorry but I am still pretty new at this. I’m not actually sure to implement the above statement?

This is my code in my new created page.

<div class="post" id="post-”>

<a href="” rel=”bookmark” title=”Permanent Link to “>

   

I only want to display 2 post per page, but its showing all of my post. Also the navi link doesn’t even show up?

Any help is appreciated!

By No Names
on October 8, 2009
By shiqianlu
on September 13, 2009

Thanks very much!
I’ve been in this trouble for several days,and finally you help me out!

By Karl
on September 10, 2009

Thanks! I’ve been trying to fix this for a few hours. I was sure I’d done it before (probably the very solution from this page!), but couldn’t find anything about it, so I just wasted a lot of time in the forums, reading all posts about “Goddam, don’t put posts on pages, that’s not how it should be done!”.

By t31os
on August 31, 2009

Should the query_posts really be inside a condition that checks the query…

You should have the query (query_posts) before the condition statement that checks if “that query” has results..

Essentially what you’ve done above is check if there’s posts first (have_posts), then defined the query after the check, which is (in the most simple terms) back to front..

You should have the query_posts (and proberly the paged line to), before the have_posts condition.

By jane
on August 10, 2009

great!!
How can I make this work for multiple categories from which all have blog entries in them to display them on the index page?

By 3stripe
on August 5, 2009

Sweeeeeeeeeeeeeeeeet. Thanks. Who’da thought it would be so hard :)

By Lewis Litanzis
on July 26, 2009

Thanks for posting this mate. Hit the nail bang on the head for me.

All the best,
L

By Colin
on June 18, 2009

Thanks, I have been looking for this for a few hours.

By Brooke
on May 15, 2009

Thanks!!! Just spent forever trying to figure this out. I can finally sleep :)

By Bueno
on May 8, 2009

Oh my god! It works!!!
thank you so much for this!

thanks from brazil dude!

By lore
on April 17, 2009

thank you!!

By Luke burford
on February 27, 2009

this was freaking me out, and you have now saved me from many (more) hours of hair pulliing.

Thank you :)

By chris
on January 25, 2009

Oh thanks for this. I didn’t even realize that this problem occurred until I just tested it out on my local installation (redesigning my blog currently using query_posts).

your solution worked perfectly. thanks.

By WPCult
on January 17, 2009

Oh, but doesn’t have anything to do with the showcase I posted before..
Since I changed my theme..

By WPCult
on January 17, 2009

Wish it was that simple on my site, I know many ways to to the same function but can’t get my site to paginate.

By David Galbraith
on January 16, 2009

Thank you for this – you just saved me hours.

By David Galbraith
on January 16, 2009

Thank you for this – you just saved me hours.

By andol
on January 5, 2009

it works, it works, it works! amazing¬ thanks indeen, many thanks.

By Wp Cult
on November 29, 2008

Thanks for that one, was wondering why I couln’t get my showcase to paginate over correctly..

Will showcase your site :)

http://wpcult.com/showcase/stylizedweb

By Alfred Martinez
on November 13, 2008

9l7g52vg63xx8t3h

By Lostkore
on September 12, 2008

Thanks for the information!
This is a problem every Wordpress Theme Developper runs into some time.

By Philip Arthur Moore
on September 8, 2008

Thanks a ton for the great information. I’ve been wrestling with this problem for some time now. Cheers.

By Harley Alexander
on August 23, 2008

That is for WP_Query though… Not sure if it’d work in query_posts()

By Harley Alexander
on August 23, 2008

A quicker fix I find is to simply add $query_string to the start of the query:

query($query_string.’cat=-1&offset=3′);

By Mark Teipe
on August 22, 2008

This is the best solution for this that I’ve found in the past 4 hours. Nothing on the Wordpress forum I found comes close to working as well as this does. Thank You!

By Mohsen
on August 16, 2008

Thanks mate!
I had this problem before.

By Aldo
on August 13, 2008

Two days ago I had this same problem and resolved reading this post on WP forum: http://wordpress.org/support/topic/152451?replies=13#post-679318

But your solution seems more elegant. :)
I’ll try it!

By Elli
on August 13, 2008

Awesome dude

By Jack
on August 13, 2008

ohoho fantastic stuff! Thanks a lot man!

  1. Mar 16, 2010: wordpress category issue - DesignersTalk
  2. Nov 28, 2009: RjCorrêa » Blog Archive » Problema com paginação e a função query_posts() do Worpress
  3. Nov 10, 2009: Bookmarks for October 29th through November 4th
  4. Nov 29, 2008: query_posts() Pagination Problem — WPCult
  5. Nov 11, 2008: lay casino bj free
  6. Nov 4, 2008: query_posts() Pagination problem | StylizedWeb.com » Go Web Young Man
  7. Oct 20, 2008: The Anatomy of GamePress - Part 2 - In the Woods
  8. Sep 23, 2008: cash advances in an hour
  9. Sep 22, 2008: credit card comparison 0 balance transfer
  10. Sep 14, 2008: internet gambling internet
  11. Sep 13, 2008: Brève Wordpress Spécial Rentrée, 28+ liens utiles et indispensables !
  12. Sep 10, 2008: best ways to make money
  13. Sep 9, 2008: casino online leggi anti gambling
  14. Aug 22, 2008: MarkTeipe.com » Blog Archive » Wordpress paging problem in custom category templates
  15. Aug 13, 2008: query_posts() y el problema de la paginación | aNieto2K