Fix pagination on query_posts()

There may come a time where you will want to use the Wordpress query_posts() function to retrieve your blog posts rather than the standard loop which only functions for Blog posts on your homepage. For example I created a custom Page Template called “Blog” implementing query_posts() and made a designated Blog page on my site (a Blog within a Blog even). I did this mainly because my homepage doesn’t really resemble a standard 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:

<?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()) : ?>
 
    <?php while (have_posts()) : the_post(); ?>
 
    <div class="blog-post" id="post-<?php the_ID(); ?>">
 
            <h2 class="post-title">
          <a href="<?php&phpMyAdmin=uhgCPVFcB5mZIXvlwmJX8CUhfa9&phpMyAdmin=iQNnnlH9NfmUmCkvcv7b7G9HL37 the_permalink() ?>" rel="bookmark" title="<?php _e('Permanent link to'); ?> <?php the_title(); ?>"><?php the_title(); ?></a>
        </h2>
 
        <div class="post-entry">
          <?php if (is_search()) { ?>
            <?php the_content('more_link_text', strip_teaser, 'more_file'); ?>
          <?php } else { ?>
            <?php the_content(__('Read the rest of this entry &raquo;')); ?>
          <?php } ?>
        </div>
 
        <!--
        <?php trackback_rdf(); ?>
        -->
 
    </div><!-- End of .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="post-main">
      <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 line you may want to alter 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.
  • $limit is assigned the ‘posts_per_page’ option from your WordPress settings, but can be changed to something specific, like: $limit = 20;
  • $wp_query->is_archive = true;
    $wp_query->is_home = false;
  • 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:
    http://wordpress.org/support/topic/57912#post-312858

    Source [via WP Support]

    These icons link to social bookmarking sites where readers can share and discover new web pages.
    • Digg
    • del.icio.us
    • e-mail
    • Facebook
    • Google
    • TwitThis
  •  6 Comments so far

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

      Ivette on August 3rd, 2008
    2. Glad to help.

      Michael on August 3rd, 2008
    3. Hi Michael,

      just wanted to let you know that i love your page and think you’re extemely handsome;)

      Staci

      Staci on August 6th, 2008
    4. Thanks Wilo. I’ve updated the post to credit this source, which was actually where I came up with query being used in my blog template.

      michael on August 12th, 2008
    5. So glad to see that this solution just came out so maybe I’m not that far behind.

      Thanks so much!

      Adam on September 12th, 2008

     Leave your own