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 comment



Thank you so much for publishing this. It finally worked!!
Pingback: WP: Pagination stays on the same page? * Ivana's Projects
Thank you so much for this solution!
Thank you so much for publishing this. It finally worked!!
Thanks for your Support, this helps me so much!
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.
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!!!!
Thank you so much for that, it was a real help!
Pingback: Problema com paginação e a função query_posts() do Worpress - Dicas e Ajudas WEB
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!
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
)
@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
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!
Thank you so much man.
I was searching for exactly what you present here.
Seriously, many thanks !!!
cool, but where should I post the change?
you really saved my ass with this, thanks very much
thanks for the solution
Nice post.. v.v thanks
Thanks a million for this. I’ve been banging my head against this problem and just lucked on to this page. Great work.
simple and understandable.. tnx!
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
This solves my problem perfectly…thanks!!
Finaly perfectly solution !!! Thanks you a lot !!!
Thanks so much! This is exactly the solution I needed.
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 !
Pingback: Links to Some Wordpress Related Articles | Learn Web Tutorials
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.
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!
Perfect!
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
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.
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.
Many thanks for this solution!
Awesome, thanks for the solution..
Thanks for that one, that did the trick for me !
Works
Thanks!
Thanks worked a treat
Thank you, This worked perfectly
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!
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?
Worked a charm thank you
Thanks. heaps, man, that had me stumped for ages… I had a custom page template to show my blog using a catagor
Thank you, This worked perfectly
thank you admin f
thanks
Great decision.. thx
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., ^_^
Hi
i was looking for this about 2 hours!!!
The fuking query_posts() in wordpress codex only have posts_per_page…
Thx a lot!
Thanks!!!
Three days looking for that… and now it seems so simple!
Great!
Very helpful, thanks!
i dont have time to read it but I know it is perfect