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.