WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a blog page at: mysite.com/blog. On this page I have this loop:


$args = array (
  'pagination'             => true,
  'paged'                  => 'pages',
  'posts_per_page'         => '5',
  'ignore_sticky_posts'    => true

<?php $query = new WP_Query( $args ); ?>

<?php if($query->have_posts()) : while($query->have_posts()) : $query->the_post(); ?>

<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>

<?php the_title('<h2 class="entry-title"><a href="' . get_permalink() . '" title="' . the_title_attribute('echo=0') . '" rel="bookmark">', '</a></h2>'); ?>

<p class="byline">
  <span class="author vcard"><?php the_author_posts_link(); ?></span> <span class="sep">|</span> 
  <abbr class="published" title="<?php the_time(__('l, F jS, Y, g:i a', 'example')); ?>"><?php the_time(__('F j, Y', 'example')); ?></abbr>
  <?php edit_post_link(__('Edit', 'example'), ' <span class="sep">|</span> <span class="edit">', '</span> '); ?>

<div class="entry-content">
  <?php the_content(); ?>
  <?php wp_link_pages('before=<p class="pages">' . __('Pages:','example') . '&after=</p>'); ?>

<p class="entry-meta">
  <span class="categories"><?php _e('Posted in', 'example'); ?> <?php the_category(', '); ?></span>
  <?php the_tags('<span class="tags"> <span class="sep">|</span> ' . __('Tagged', 'example') . ' ', ', ', '</span>'); ?> 
  <span class="sep">|</span> <?php comments_popup_link(__('Leave a response', 'example'), __('1 Response', 'example'), __('% Responses', 'example'), 'comments-link', __('Comments closed', 'example')); ?> 

Which works as expected, however I cannot get pagination to work. I have tried a variety of options. Any help is appreciated.

share|improve this question
up vote 0 down vote accepted

This works for me with no 404s

 $args = array (
    'posts_per_page' => 5 ,
    'paged' => $paged,
    'ignore_sticky_posts'    => true
  $blog_query = new WP_Query($args);
  if ( $blog_query->max_num_pages > 1 ) { 
     next_posts_link('&larr; Older posts', $blog_query->max_num_pages); 
     previous_posts_link('Newer posts &rarr;', $blog_query->max_num_pages); 
  while ( $blog_query->have_posts() ) {

     // STUFF
     if ( $blog_query->max_num_pages > 1 ) { 
     next_posts_link('&larr; Older posts', $blog_query->max_num_pages); 
     previous_posts_link('Newer posts &rarr;', $blog_query->max_num_pages); 

I can see the need for a secondary loop if you want to not use category pages or want there to be editable content on the page or a title different than the category name.

share|improve this answer
This alone is almost certainly not going to work because this is a secondary loop and the ordinary pagination functions do not work well with secondary loops, plus the main query will be out of sync with the secondary query so you are likely to get 404s. – s_ha_dum Jun 28 '13 at 19:25
I added some info – eteich Jun 28 '13 at 20:11
next_posts_link assumes the main query, so are paginating on a different query than you are using to display the posts. The same is true with previous_posts_link. There will be problems because of that. – s_ha_dum Jun 28 '13 at 20:22
I'm specifying the blog query in it – eteich Jun 28 '13 at 20:26
This seems to be working for me so far. – Scott Simpson Jun 28 '13 at 20:41

You should not need to create a secondary Loop. Besides causing additional work for the server, the ordinary pagination functions do not work well with secondary loops.

Use pre_get_posts to alter the main query.

function pregp_wpse_104648($qry) {
  if (is_main_query() && $qry->is_home()) {

I believe that is all you need.

Note: I am guessing at the conditions you need. if (is_main_query() && $qry->is_home()) { may be wrong, but that is the idea.

share|improve this answer
If I don't create the loop, my posts don't display. All I get is the title of the page (where it should be the title of the post). I am using a custom template (blog.php) and under settings/reading posts page is set to 'blog'. – Scott Simpson Jun 28 '13 at 19:59
You need the Loop, what you don't need is a secondary Loop-- the new query--, unless you are doing something odd with the blog index. – s_ha_dum Jun 28 '13 at 20:03
Sorry -- I think I should have said query. Without the query, it doesn't work. – Scott Simpson Jun 28 '13 at 20:41
Where exactly are you trying to use this? – s_ha_dum Jun 28 '13 at 20:53
mysite.com/blog. There is a template page set for the 'blog' page. It is named 'blog.php'. Under settings/reading posts page is set to 'blog'. – Scott Simpson Jun 28 '13 at 21:05

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.