scriptflipper.com is the portfolio website of Michael Ciccarelli, freelance web designer and developer

contact@scriptflipper.com / +1 (702) 427-7766

Fix pagination on query_posts()

This post was published Monday July 14, 2008 filled under code.

 
Post Meta
  • Jul 14, 2008
  • 43 comments

  • Tags
  • Here is a simple solution for those of you looking to customize your WordPress theme, to use the WordPress query_posts() function to retrieve your blog posts rather than the standard loop.

    I created a custom Page Template named “Blog” using query_posts() and assigned this template to be used on a designated Blog page within my site at http://michaelciccarelli.com/blog/ (a Blog within a Blog even). I did this mainly because my original homepage didn’t really resemble a traditional blog and I wanted something more than just your average archives page that only lists the titles of all your previous blog posts.

    So here’s my Blog Page Template’s Source Code:

    
    <?php
    /*
    Template Name: Blog page
    */
    ?>
    <?php get_header(); ?>
    <div id="content">
      <div id="primary">
        <?php
        $limit = get_option('posts_per_page');
        $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
        query_posts('showposts=' . $limit . '&paged=' . $paged .'&cat=-166,-167,-168');
        $wp_query->is_archive = true; $wp_query->is_home = false;
        ?>
        <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
        <div class="blog-post" id="post-<?php the_ID(); ?>">
          <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
          <div class="post-entry">
              <?php the_content(__('Read the rest of this entry &raquo;')); ?>
          </div>
        </div><!-- /post -->
        <?php endwhile; ?>
    
        <!-- Page Navigation -->
        <?php if (function_exists('wp_pagenavi')) : ?>
        <div class="pagenavi">
            <?php wp_pagenavi(); ?>
        </div>
        <?php else : // Use WordPress default page navigation. ?>
        <div class="pages">
            <span class="older"><?php next_posts_link('&laquo; Older Entries'); ?></span>
            <span class="newer"><?php previous_posts_link('Newer Entries &raquo;'); ?></span>
        </div>
        <?php endif; ?>
        <?php else : ?>
        <div class="blog-post">
            <h2 class="post-title"><?php _e('404 - Not Found'); ?></h2>
            <div class="post-entry">
              <p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
              </div>
      </div>
      <?php endif; ?>
          </div>
          <?php get_sidebar(); ?>
    </div><!-- content -->
    <span class="clr"></span>
    <?php get_footer(); ?>
    

    Notes:

    The only bit you may want to alter in the function that retrieves your posts is the 3rd line in from top:query_posts('showposts=' . $limit . '&paged=' . $paged .'&cat=-166,-167,-168');

    Notice the &cat=-166,-167,-168 this basically says exclude the following category IDs, which in my case are portfolio categories, that I don’t want to display in my blog section. This parameter is more commonly used like cat=1,3,5 which will only return posts from those categories.

    $limit is assigned the ‘posts_per_page’ option from your WordPress settings, this stopped working for me for some reason but not a big deal because it can be changed to something specific, like: $limit = 20;

    
    $wp_query->is_archive = true;
    $wp_query->is_home = false;
    

    These variables that come after the query_posts() are important as they force posts_nav_link() (and so pagination) to work, along with a few other helpful results gained for fooling WordPress into thinking we’re in the archive pages.

    For the $paged stuff, see: this support forum postSource [via WordPress Support]

     
     

    43 Responses to “Fix pagination on query_posts()”

    Leave a response

     

    Ivette says:

    Aug 03, 2008, 3:15 pm:

    thanks a lot, you’ve saved my day :)

     

    Michael says:

    Aug 03, 2008, 4:56 pm:

    Glad to help.

     

    Adam says:

    Sep 12, 2008, 6:07 am:

    So glad to see that this solution just came out so maybe I’m not that far behind.

    Thanks so much!

     

    james says:

    Oct 18, 2008, 3:15 pm:

    Fantastic! I’ve been searching for the solution to this for several hours. Many thanks :-)

     

    James says:

    Oct 20, 2008, 1:11 am:

    Hey… just found this post after looking for a little while thanks. Wanted to add i was using a plugin called redirection to handle when i rename posts and also to help remove the category base from the permalink url.

    The plugin was causing a 404 when going to page 2. I had to disable one of it’s redirect matches. I haven’t ascertained whether it will throw off the rest of my site… we shall see.

    Thanks for the help, led me on the right path!

     

    Kyle says:

    Oct 30, 2008, 8:40 am:

    Just a simple: Thanks!

     

    Website Directory - Class Pages says:

    Dec 14, 2008, 7:27 pm:

    [...] Fix pagination on query_posts() | MikeChick.net reddit_url=’http://www.baby-parenting.com/lma/directory/Kids_&_Teens/School_Time/Science/Chemistry/Class_Pages/Class_Pages.html’ reddit_title=’Website Directory – Class Pages’ [...]

     

    Will Dawson says:

    Dec 31, 2008, 8:05 pm:

    Thank you so much! Been searching for this for about 3 days.

     

    RobInjection says:

    Jan 07, 2009, 9:06 pm:

    Dude, thank you sooooo much. I spent 3 hours trying to figure this out and almost pulled my hair out. You are a king amongst men! ;)

     

    Caroline says:

    Jan 17, 2009, 4:32 pm:

    Thank you so much for this. As like the others, I was searching for a straight-forward answer for a while.

    My only problem is using WordPress’ option of using a static homepage. On the page that I selected as the Posts page, the page links break. When I click on a different page number, I get redirected to my 404 error page.

    Do you know how to solve this problem?

    It may help to know that I am also using the Advanced Category Excluder plugin by DjZoNe. However, even when I tell it not to exclude any categories for both the home and archive pages, it still doesn’t work.

    This is my query_posts function:

    query_posts( array(
    “showposts” => $limit,
    “paged” => $paged,
    “cat” => implode(“,”, get_archiveCats(1))
    ));

    I created a function that allows me to dynamically change which categories are included in the archive list.

    I hope this makes sense, and any help will be awesome.

     

    Nicole says:

    Feb 17, 2009, 7:49 am:

    I’m probably a bit late on this one..
    Thank-you!!

    p.s: really like the look of your site- very clean and simple- love the transparency.

     

    Sebas van den Brink says:

    Feb 18, 2009, 2:01 pm:

    Thank you so much! The explanation of query_posts on the WP Codex is faulty. Merely placing query_posts(“cat=-4″) doesn’t work. Your post is gold!

     

    Sebas van den Brink - grafisch ontwerp, vormgeving, video/animatie, illustratie » Blog Archive » Fixed pagination on query_posts() with an excluded category and/or correct dates. says:

    Feb 18, 2009, 2:51 pm:

    [...] As a very useful way of excluding a category from the index. But it doesn’t work so well on archives. Especially not when these are paginated. The problem was that now every older or newer entry was actually the same. Thankfully, the brilliant Michael Ciccarelli, figured out a way to overcome this problem: [...]

     

    Mike says:

    Feb 22, 2009, 3:41 pm:

    This is some good stuff. Nice site too. I was just looking for a way to include pagination, so thanks.

     

    Fondos gratis says:

    Feb 25, 2009, 11:41 pm:

    excelent my friend!!!!!!!!!!!!!!!!!!!!! eres un genio, very god job!! :) )))))))

     

    Rob Anderson says:

    Mar 01, 2009, 4:50 am:

    Thank you so much – this was exactly the probelm I ws trying to solve :)

     

    coco says:

    Mar 07, 2009, 7:31 pm:

    thanks a lot.
    now I can list all my posts on a page template.

    if someone has problem read more content, he/she should insert the following

    global $more;
    // set $more to 0 in order to only get the first part of the post
    $more = 0;

     

    WordPress: hacking categories : Fuyoh! says:

    Mar 29, 2009, 2:53 am:

    [...] Michael Ciccarelli has a better solution. [...]

     

    Daniel says:

    Apr 17, 2009, 1:05 pm:

    Hi Michael, this fix is working fine, but when using a numbered pagination with a plugin like “wp-page-numbers” there are empty pages because excluded items are included in the total count. Do you have any idea how to fix that? Thank you!

     

    OpenGraphicDesign.com says:

    Apr 18, 2009, 10:21 am:

    Based on examples I found on WordPress and the solution on this post, here’s a simpler version that worked for me. Thanks for posting this information, was a lot of help. – Henry

     

    Michael says:

    Apr 21, 2009, 5:39 am:

    @daniel…shoot me an email I’ll take a look for you…anyone else having problems I’m much more responsive via email…sorry for the delay getting back to you(s)

    Mike
    mike@badapplemedia.com

     

    Galo says:

    May 20, 2009, 6:20 am:

    Very nice just solved my problem ROCK ON!

     

    Trey says:

    May 29, 2009, 10:08 am:

    Mike – thanks so much for this post. I created my first new template for WordPress and couldn’t figure out why pagination wasn’t happening. I was able to get it working immediately after reading your post.

    You rock!

     

    budi says:

    May 29, 2009, 6:31 pm:

    you RAWKS

     

    Joe says:

    Jun 13, 2009, 11:01 pm:

    Thanks, this saved me a lot of time.

     

    Lorenzo De Tomasi says:

    Jun 18, 2009, 1:06 pm:

    Thank you very much!
    The only “bug” I have found is that it displays the whole post, not only until the , also if there is “”. How can we solve it?
    Thanks

     

    Isotype» Blog Archive » WordPress posts archive page template says:

    Jun 26, 2009, 12:15 pm:

    [...] Fix pagination on query_posts(), MikeChick.net [...]

     

    Frontiere digitali » WordPress posts archive page template says:

    Jun 27, 2009, 12:33 am:

    [...] Fix pagination on query_posts(), MikeChick.net Aggiungi un avatar alla tua firma (gratuito e valido per tutti i blog): http://en.gravatar.com/ [...]

     

    Mike says:

    Jun 27, 2009, 3:19 am:

    Thank u guy,

    nice article, you helped me so much with this information!

     

    Steph says:

    Jul 10, 2009, 11:51 pm:

    Thank you so much! I was tearing my hair out trying to figure out how to get pagination and still separate my posts by categories. This totally saved my sanity.

     

    Pagination With Multiple Loops Not Cycling Through Posts - WordPress Tavern Forum says:

    Jul 19, 2009, 12:21 am:

    [...] a look at this: http://michaelciccarelli.com/2008/07/fix…n-query-posts/ Make sure you run get_query_var() before ANY query_posts on the page though. heres a sniplet of [...]

     

    Fixed Pagination On Query_posts() « blog.bldng. says:

    Jul 19, 2009, 9:09 am:

    [...] As a very useful way of excluding a category from the index. But it doesn’t work so well on archives. Especially not when these are paginated. The problem was that now every older or newer entry was actually the same. Thankfully, the brilliant Michael Ciccarelli, figured out a way to overcome this problem: [...]

     

    Fix pagination on query_posts() | MikeChick.net says:

    Jul 19, 2009, 4:14 pm:

    [...] original post here: Fix pagination on query_posts() | MikeChick.net Tags: appid – detected – invalid – missing Comments0 Leave a Reply Click here to cancel [...]

     

    Jonathan Thirkill says:

    Jul 29, 2009, 1:11 pm:

    Thanks for this! Now I don’t have to be forced to list all my posts in one big long page.

    Thank You!

     

    Jess says:

    Sep 16, 2009, 1:54 am:

    Thank you thank you thank you! You saved my butt with that.

     

    andrew says:

    Oct 27, 2009, 12:23 am:

    thank you so much. this helped a lot.

     

    Mike Watts says:

    Nov 20, 2009, 1:26 pm:

    Thank you so much!!!

     

    Dan says:

    Dec 03, 2009, 11:52 am:

    THANK YOU! Wish I found this 3 hours ago lol

     

    Randy says:

    Dec 04, 2009, 4:16 am:

    Immensely helpful. Many thanks for this straightforward and well-written explanation.

     

    Nashville Website Designer says:

    Jan 26, 2010, 11:03 am:

    Man you rock. I was getting very frustrated with this pagination mess. thanks a bunch for the enlightenment.

     

    RomainB says:

    Mar 22, 2010, 6:40 am:

    Thank you SO much!

     

    wp-popular.com » Blog Archive » Fix pagination on query_posts() / scriptflipper.com says:

    May 08, 2010, 10:22 pm:

    [...] link: Fix pagination on query_posts() / scriptflipper.com Tags: [...]

     

    sujit says:

    May 23, 2010, 7:17 am:

    hi,

    thanks a lot. i tried this code, its very useful.
    1st of all i am not web developer or have not taken any training. I just do it by reading instruction.

    my problem is this code show full posts by default (at least for my site. I want it show read more tag after few lines of my post.

     

    Leave A Comment