Thanks!!! Just spent forever trying to figure this out. I can finally sleep
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.
Thanks!!! Just spent forever trying to figure this out. I can finally sleep
Oh my god! It works!!!
thank you so much for this!
thanks from brazil dude!
thank you!!
this was freaking me out, and you have now saved me from many (more) hours of hair pulliing.
Thank you
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.
Oh, but doesn’t have anything to do with the showcase I posted before..
Since I changed my theme..
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.
Thank you for this - you just saved me hours.
Thank you for this - you just saved me hours.
it works, it works, it works! amazing¬ thanks indeen, many thanks.
Thanks for that one, was wondering why I couln’t get my showcase to paginate over correctly..
Will showcase your site
9l7g52vg63xx8t3h
Thanks for the information!
This is a problem every Wordpress Theme Developper runs into some time.
Thanks a ton for the great information. I’ve been wrestling with this problem for some time now. Cheers.
That is for WP_Query though… Not sure if it’d work in query_posts()
A quicker fix I find is to simply add $query_string to the start of the query:
query($query_string.’cat=-1&offset=3′);
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!
Thanks mate!
I had this problem before.
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!
Awesome dude
ohoho fantastic stuff! Thanks a lot man!
on June 18, 2009
Thanks, I have been looking for this for a few hours.