Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

My pagination links are failing for a custom post type here http://mjw.view-wireframes.com/about-us/press/

the page reloads http://mjw.view-wireframes.com/about-us/press/page/2/ but this has no effect on the displayed posts. Flushing the permalink structure has no effect so my guess is the problem is in the query.

<?php 
$paged = (get_query_var('paged')) ? get_query_var('paged') : 10;
$temp = $wp_query;
$wp_query= null;
$wp_query = new WP_Query();
$wp_query->query(array( 'post_type' => 'press',
                                'orderby'   => 'post_date',
                                'posts_per_page' => $paged,
                                ));
while ($wp_query->have_posts()) : $wp_query->the_post(); 



    // The following determines what the post format is and shows the correct file accordingly
    $format = get_post_format();
    get_template_part( '/lib/includes/post-formats/'.$format );

    if($format == '')
    get_template_part( '/lib/includes/post-formats/standard' );

endwhile;


global $wp_query;

$big = 999999999; // need an unlikely integer

echo paginate_links( array(
    'base' => str_replace( $big, '%#%', get_pagenum_link( $big ) ),
    'format' => '?paged=%#%',
    'current' => max( 1, get_query_var('paged') ),
    'total' => $wp_query->max_num_pages
) );

$wp_query = null; $wp_query = $temp; ?> 

Pagination function

<?php $wp_query = null; $wp_query = $temp;?> 

function pagination($pages = '', $range = 4)
{
     $showitems = ($range * 2)+1;  

     global $paged;
     if(empty($paged)) $paged = 1;

     if($pages == '')
     {
         global $wp_query;
         $pages = $wp_query->max_num_pages;
         if(!$pages)
         {
             $pages = 1;
         }
     }   

     if(1 != $pages)
     {
         echo "<div class=\"theme-pagination\"><ul><li><span>Page ".$paged." of ".$pages."</span></li>";
         if($paged > 2 && $paged > $range+1 && $showitems < $pages) echo "<li><span><a href='".get_pagenum_link(1)."'>&laquo; First</a></span></li>";
         if($paged > 1 && $showitems < $pages) echo "<li><span><a href='".get_pagenum_link($paged - 1)."'>&lsaquo; Previous</a></span></li>";

         for ($i=1; $i <= $pages; $i++)
         {
             if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems ))
             {
                 echo ($paged == $i)? "<li><span class=\"current\">".$i."</span></li>":"<li><span><a href='".get_pagenum_link($i)."' class=\"inactive\">".$i."</a></span></li>";
             }
         }

         if ($paged < $pages && $showitems < $pages) echo "<li><span><a href=\"".get_pagenum_link($paged + 1)."\">Next &rsaquo;</a></span></li>";
         if ($paged < $pages-1 &&  $paged+$range-1 < $pages && $showitems < $pages) echo "<li><span><a href='".get_pagenum_link($pages)."'>Last &raquo;</a></span></li>";
         echo "</ul></div>\n";
     }
}
share|improve this question

2 Answers 2

  1. Store your new WP_Query() in a variable
  2. Globalize $wp_query
  3. Move the $wp_query swap-hack to after your new WP_Query() generation
  4. Pass the variable to $wp_query, not the new WP_Query() call itself.

e.g.

<?php 
// Custom query args
$press_query_args = array(
    'post_type' => 'press',
    'orderby'   => 'post_date',
    //'showposts' => '10',
    'posts_per_page' => $paged,
);
// Custom query
$press_query = new WP_Query( $press_query_args );

// Globalize $wp_query
global $wp_query;
// Swap-hack
$temp = $wp_query;
$wp_query= null;
$wp_query = $press_query;

// Output custom loop
while ($press_query->have_posts()) : $press_query->the_post(); 

// EVERYTHING FROM HERE BELOW SHOULD BE THE SAME,
// THOUGH I'VE CLEANED IT UP A BIT
// (MAINLY, REMOVING THE CLOSE/OPEN PHP TAG COMBOS)

// The following determines what the post format is and shows the correct file accordingly
$format = get_post_format();
get_template_part( '/lib/includes/post-formats/'.$format );

if($format == '')
get_template_part( '/lib/includes/post-formats/standard' );

endwhile;  

if (function_exists("pagination")) {pagination($additional_loop->max_num_pages);} 

$wp_query = null; $wp_query = $temp;
?> 

EDIT

Re: your pagination/post-per-page issue:

Hi chip, thank you, but this produces some odd behavior, on the first page you see 10 articles and then pagination from pages 1-7, but on the second page you get the first two articles that appear on page 1 and the pagination numbering changes to go from 2-35

I suspect the problem lies here:

$paged = (get_query_var('paged')) ? get_query_var('paged') : 10;

Couple with this array argument:

$wp_query->query(array( 
    'posts_per_page' => $paged,
));

What happens if you remove $paged and the 'posts_per_page' array argument?

ALSO:

The pagination (1-7 vs 2-35) issue is probably separate from the posts-per-page issue (though perhaps related). Try to use previous_posts_link() and next_posts_link(), just to isolate the posts-per-page issue.

share|improve this answer
    
Hi chip, thank you, but this produces some odd behavior, on the first page you see 10 articles and then pagination from pages 1-7, but on the second page you get the first two articles that appear on page 1 and the pagination numbering changes to go from 2-35. –  toomanyairmiles Nov 30 '11 at 14:51
    
I believe that is a separate problem, possibly caused by this array key in your custom query args array: 'posts_per_page' => $paged,? –  Chip Bennett Nov 30 '11 at 14:55
    
Ah, I've changed that variable to 10 and commented that line of code out, it was referring a value set in the wordpress front end. –  toomanyairmiles Nov 30 '11 at 14:58
    
Where does the pagination() function come from? A Plugin (I assume), or custom Theme function? –  Chip Bennett Nov 30 '11 at 15:01
    
It's a custom function to modify the way pagination displays, I've added the function code above. –  toomanyairmiles Nov 30 '11 at 15:13
up vote 0 down vote accepted

The answer turned out to be using orderby date instead of post date!

share|improve this answer

Your Answer

 
discard

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.