StylizedWeb is a web design trends 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

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



Talk of the town
  • Renea: Earning an extra commission check help put my kids through private school! No doubt it
  • South West Balloon Flights: Thanks for the script. Looks nice.
  • Tony: How about WP Super Cache. Google now takes into account a site’s loading time and what better way to...
  • Emek Elektrik: Thanks Adminstrator For Dar Gelirli Users…
  • Nelly: That is a good article for css thank you admin
Amigos

What do you think?





56 Responses so far

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