Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress

Kevin Korte
Kevin Korte
28,149 Points

Paginating custom post types

Has anybody had pretty serious issue paginating custom post types off a WP_Query object?

I've spent the better part of the day doing so, and I can not get it to work. I've tried so many solutions, and even stripped down loops won't work.

If I change the paged variable by hardcode, and refresh, I see the second, third, etc page of posts, but I can't get paginate_links(); to work correctly. If I click on the pagination link it throws it back to the index file. I've tried every sample from the codex, and feel like I've worn google, wptuts, and stackexchange.wordpress out, and no dice.

More so just looking to see if others have had as hard time as I have, curious to see if you had any solutions that worked for you that I could try or retry. Not really asking for specific help. I'm done with this issue for the day.

4 Answers

Kevin Korte
Kevin Korte
28,149 Points

Spent the morning getting everything reset back up on the default loop. Pagination now works, which allowed me to set up Paul Irish's infinite scroll plugin. All is well and working now.

However, I had the exact same issue with my pagination at first on the default loop, and flushed my permalinks, refreshed the browser, and all worked fine. I never tried flushing my permalinks on the custom post type.

I may go back and do a test and try that.

John Locke
John Locke
15,479 Points

I believe you have to flush the permalinks every time you change the permalink structure.

John Locke
John Locke
15,479 Points

Kevin:

Can you post the template code you're having an issue with?

Kevin Korte
Kevin Korte
28,149 Points

Hi John,

Here is what I've been trying.

<?php
            $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
            $args = array(
                       'post_type' => 'projects',
                       'posts_per_page' => 5,
                       'paged' => $paged
                        );

        $the_query = new WP_Query( $args );
?>

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

           //General Loop Stuff

  <?php endwhile; else: ?>
               <p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
 <?php endif; ?>

  <?php
           global $the_query;  
           $total_pages = $the_query->max_num_pages;  

       if ($total_pages > 1){  

        $current_page = max(1, get_query_var('paged'));  

    echo '<div class="page-numbers-centered">';

    echo paginate_links(array(  
      'base' => get_pagenum_link(1) . '%_%',  
      'format' => '/page/%#%',  
      'current' => $current_page,  
      'total' => $total_pages,
      'type' => 'list',
      'prev_text' => '<<',
      'next_text' => '>>',
    )); 

    echo '</div>';
} 
?>

The currently creates the correct number of pagination links, but doesnt work when I click on it. If I manually update the $paged variable from 1 to 2 and refresh, I do get the second page of pagination but the URL clearly stays the same. I have a missing link between my pagination URL and updated the $paged variable to the correct page.

I've tried using $wp_query on everything instead of $the_query after a suggestion on WP.SE and didn't change anything (didn't expect it to)

I have tried this http://css-tricks.com/snippets/wordpress/paginate-custom-post-types/ and no dice.

I saw this, but was not interested in using this as a fix. It's a 2 year old fix and there has to be a better and less hacky way. http://wp.tutsplus.com/tutorials/custom-post-type-pagination-chaining-method/

This is where I got my pagination code, which I've used before on a different project with the default post type and it worked beautifully. http://wp.tutsplus.com/tutorials/wordpress-pagination-a-primer/?search_index=4

I've tried this:

<?php
             $big = 999999999; // need an unlikely integer

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

which I found here: https://codex.wordpress.org/Function_Reference/paginate_links and still no dice.

This is were I've hit my PHP wall. I've looked at the core files to see what is inside the paginate links function I called, but still can't see where my disconnect is between the paged variable and my pagination link.

For this very project, I've decided to move this custom post type over to the default post type, add my custom fields to that, and be done with it. I do believe that this post type is the only one I will need to paginate. The rest wont' be an issue I don't think. However, I would still love to find my error and a fix to this because I know it will be a problem in future project. There has to be an easy way to paginate custom post types as easily as the default post type.

Kevin Korte
Kevin Korte
28,149 Points

I tried pagenavi yesterday, just tried paginate now, and I can't get either plugin to generate any pagination links. Using the default settings and default calls I get nothing. I feel like the solution is just right there, but I can't see it.

Thank you for those two suggestions though, I'll keep playing with them.

Matt Campbell
Matt Campbell
9,767 Points

You'd be surprised just how often re-saving the permalinks can solve a whole host of problems.