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

How do I add pagination for a custom page listing custom post types?

I'm having a lot of troubles with pagination. I created a custom page called page-blog.php. I am trying to display four blog posts per page. This is the code I currently have.

<section class="wrapper">

    <section class="blog-main-content">
        <h2>Blog</h2>
        <?php $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1; ?>

        <?php $loop = new WP_Query( array( 'post_type' => array('blog', 'post'), 'posts_per_page' => '4', '&paged' => $paged ) ); ?>

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

            <div class="blog-individual-post">
                <a href="<?php the_permalink(); ?>"><h2><?php the_title(); ?></h2></a>

                <section class="blog-individual-date-and-author">
                    <?php the_time('F jS, Y'); ?> by <em><?php the_author_posts_link(); ?></em>
                </section>

                <section class="blog-numbered-comments">
                    This post currently has
                    <a href="<?php comments_link();?>"><strong><?php comments_number( 'no comments', '1 comment', '% comments' ); ?></strong></a>
                </section>

                <div class="blog-entry">
                    <?php the_content(); ?>
                </div>
            </div>
        <?php endwhile; endif; ?>

        <?php

            // next_posts_link() usage with max_num_pages
            next_posts_link( 'Older Entries', $loop->max_num_pages );
            previous_posts_link( 'Newer Entries' );
        ?>

    </section>

</section>

The code properly displays the four posts on domain.com/blog/ but if I click on "Older Entries" at the bottom I get a 404 page. The link goes from domain.com/blog/page/2

I have tried a lot of tutorials online and have not been able to find an answer to this. Is there a specific video I can be pointed to that explains pagination on a custom page with custom post types?

Please let me know if you need more information. Thank you.

1 Answer

Stephen O'Connor
Stephen O'Connor
22,291 Points

I'd like to know the answer to this as well, recently I done a project where I had to paginate custom post types and got the same issue as you Michael. I usually use this code to paginate all my posts in WordPress but on this occasion I could not get it to work. Any input from Zac Gordon or any other person with more expertise than me would be much appreciated.

I figured it out. I was using a custom post type called "blog" and I also had a custom page named "page-blog". Because they are both named the same Wordpress gave me the 404 error no matter what I did.

I ended up eliminating the blog custom post type and am just using posts for that now.