Fix previous_posts_link() And next_posts_link() For Custom Post Types

As I update the DesignRJC site, I run into a query issue for my Custom Post Types in WordPress 3.1 where the previous_posts_link() and next_posts_link() are not displaying properly. If you’ve been working with custom post types in WordPress 3.1 and have this issue, hopefully this will help resolve it.

Researching The Issues With previous_posts_link() And next_posts_link

Basically, the previous_posts_link() and next_posts_link() functions allow you to display the next set of posts being queried. In this case, it is a custom post type of portfolio items I created for DesignRJC to display projects, client work and concepts. You can read more on using the these functions on the WordPress Codex here and here.

Its maddening when something so simple as using a built-in WordPress function does not work properly and I had several hunches to try and debug.

At first, I thought it was the theme.  Currently testing the capabilities of building out a child-theme using Justin Tadlock’s Prototype Theme. Unfortunately, not being an exclusive member of their support forums doesn’t get me much help.  Still not 100% sold on child-themes and relying on a framework but it definitely has its positives.

Began double-checking the custom post type registration functions to make sure the re-write slug name for portfolio post type is different than the page template name used.

Everything checked out okay on that end as well.

I then study the query itself within the page template.  The query I use for the portfolio post type is:

<?php 
    $paged = ( get_query_var( 'paged' ) ) ? get_query_var('paged') : 1;  	
    $args = array( 'post_type' => 'portfolio', 'posts_per_page' => 2, 'paged' => $paged );
    $loop = new WP_Query( $args );
    while( $loop->have_posts() ) : $loop->the_post();
?>

From reading other people’s experiences, it seems that my query is correct because of creating the variable $paged = ( get_query_var( ‘paged’ )) ? get_query_var( ‘paged’ ) : 1; line of code. This didn’t resolve my problem.

Solution For Fixing previous_posts_link() And next_posts_link() Properly For Custom Post Types

I came across another solution for displaying next_posts_link() and previous_posts_link() functions correctly.  It required the use of a second parameter of $max_number_pages. In short, it passes the total number of pages to be paginated within this custom query. This solved my issues and here is the snippet of the code I used:

<?php previous_posts_link('« Previous', $loop->max_num_pages); ?>
<?php next_posts_link('More »', $loop->max_num_pages); ?>

Notice in both functions, the second parameter is used.  By default, it is set to zero to display all pages.  Pagination for custom post types require extra finesse.

Where it is $loop->max_num_pages, $loop is the original query variable object from above and by using max_num_pages, I pass the total number of pages to be paginated into the second variable from the query.

Very helpful hint and fix to working with WordPress custom post types. Credit goes to WPCanyon for coming up with this solution.

21 thoughts on “Fix previous_posts_link() And next_posts_link() For Custom Post Types

  1. Thank you, thank you, thank you. You have saved the day for me I have tried countless solutions to solve this issue with custom post types and trying to get them to paginate, and yours is the winner for me. You are a lifesaver.

    • Thank Google for indexing my page so your able to find it. Yeah, its one of those quirky WordPress codes that need a bit of finessing. Definitely for those one off projects that need custom post types. Glad it helped.

  2. This pagination issue was driving me crazy as I could’t find a solution for days. Gladly I finally came across this post.
    Thanks so much for the above solution!

  3. Thank you sooo much :) I have been through so many tutorials today trying to figure this out…I almost gave up. Thank you for putting the time in to explain it :)

  4. Thank you very much! This is the solution I was leaning towards as well after browsing through the WordPress core and analyzing the implementation of the previous_posts_link and next_posts_link functions. It’s comforting to see that someone else is using this method as well!

  5. I’ll join the chorus of Thank Yous. Was having trouble with this issue – and it took the proper Google search to find your post, but you solved my issue perfectly.

    Cheers!

  6. Could not be more glad I came across this post. I searched high and low for a viable solution to this issue, and this was the only one that resulted in success. My particular solution required a more custom solution, but this is what made it possible!

    Thank you.

  7. My client’s blog was working just fine and then “suddenly” there was this issue with the pagination. Your solution was finally the one to get us back on track – thank you!!!

  8. As the others have said: thank you, thank you, thank you! I have been researching this for the last few hours because it simply refused to cooperate, now it works perfectly :)

  9. Well, the links are displaying, but I have a huge problem! They lead to a 404 error! All the other next and previous links are working on my website, and I have multiple custom post types. Only on one of them throws me a 404 and I have no idea why! I’ve checked everything, I’ve tried a lot of things… It wont work anyhow! Any help is much appreciated!

    • Hi mate, just been trough that recently. It’s because your custom post type slug is the same as your page. If for example your page is Portfolio the the page’s slug will be “portfolio” and if your Custom Post Type’s slug is also “portfolio” the pagination will end up with 404 error.

      This sorted my 404 problem ;)

  10. Thanks for the tutorial! I can get the next_posts_link and previous_posts_link to work, but cannot get the next_post_link and previous_post_link (post without an “s”) to work on a single-{post_type}.php page. Any thoughts?

  11. Great tutorial. Is it require to set the next/prev for custom post type inside the loop or can in just stay outside?

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>