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!
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.
Have some feedback? Leave a commentThanks! 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!
Thanks!! I’ve been struggling with this for a couple of days!
Man, you saved my day! Awesome.
RAD! Thank you so much.
thanks a lot gr8 work……….
Money. And the 50th comment. All I need is a scotch and a blowjob and I’m set.
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!
Thanks works great!
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’));
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!
It wont allow me to post my code
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!
Thanks very much!
I’ve been in this trouble for several days,and finally you help me out!
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!”.
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.
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?
Sweeeeeeeeeeeeeeeeet. Thanks. Who’da thought it would be so hard
Thanks for posting this mate. Hit the nail bang on the head for me.
All the best,
L
Thanks, I have been looking for this for a few hours.
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 February 1, 2010
Thanks! Nice trick