StylizedWeb is a blog about web trends and tutorials, KnowHow community forum now maintained by Ross Johnson who also runs a web design company and design blog.

Categories Archive

Popular Posts
Advertisement
Premium WordPress Themes, Theme Galaxy Basecamp
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.

What do you think?





23 Responses so far

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