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

Michael Cain
PLUS
Michael Cain
Courses Plus Student 16,547 Points

Pagination with WP_Query & Custom Post Type

Hi,

Hopefully someone can shed some light on this. I'm trying to set up pagination for a custom post type (which uses the Custom Post Type UI Plugin) but can't get it to work for the life of me! I get a 404 error on page 2 every time.

Here's my code...

<?php

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

        $args = array(
            'post_type' => 'cemeteries',
            'orderby' => 'title',
            'order' => 'ASC',
            'posts_per_page' => 30,
            'paged' => $paged
        );

        $the_query = new WP_Query($args);

    ?>

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

    <!-- options -->
    <div class="col-md-12 options border-bottom">

        <!-- pagination -->
        <ul class="pagination pull-right">
            <li><?php echo get_next_posts_link( 'Next Page', $the_query->max_num_pages ); ?></li>
            <li><?php echo get_previous_posts_link( 'Previous Page' ); ?></li>
        </ul>

    </div>
    <!-- /.options --> 

    <!-- cemeteries -->
    <div class="cemeteries">

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

            <!-- cemetery -->
            <div class="col-md-12">

                <div class="col-md-4">
                    <?php
                        if ( has_post_thumbnail() ) {
                            the_post_thumbnail('', array('class' => 'img-responsive'));
                        }
                    ?>
                </div>

                <div class="col-md-8">
                    <h2><?php the_title(); ?></h2>
                    <?php the_content(); ?>
                    <p><a href="<?php the_permalink(); ?>"><button>Visit Cemetery</button></a></p>
                </div>

            </div>
            <!-- /.cemetery -->

        <?php endwhile; ?>

    </div>
    <!-- /.cemetries -->

    <!-- options -->
    <div class="col-md-12 options border-bottom">

        <!-- pagination -->
        <ul class="pagination pull-right">
            <li><?php echo get_next_posts_link( 'Next Page', $the_query->max_num_pages ); ?></li>
            <li><?php echo get_previous_posts_link( 'Previous Page' ); ?></li>
        </ul>

    </div>
    <!-- /.options -->  

    <?php wp_reset_postdata(); ?>

    <?php else:  ?>

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

    <?php endif; ?>

This code is on a page called Cemeteries and the CPT is also called Cemeteries with the same slug - could this be the problem with the pagination??

Mick

3 Answers

Michael Cain
PLUS
Michael Cain
Courses Plus Student 16,547 Points

Hi Steve,

Sorted! Thanks for your help! I also had to set the parameter "With_Front" to True in order to get it working :)

Much appreciated!

Regards Michael

Glad to hear it, something to remember for myself too.

Hi Michael

Your code looks fine to me, although I haven't had chance to take a look into greater detail.

It could be down to your permalinks, if you go into the wordpress admin panel and go into permalinks, under settings and save them. This usually fixes the problem, if it doesn't I'll see if I can look into it further for you.

Hopefully this helps, sorry for the quick response!

Michael Cain
PLUS
Michael Cain
Courses Plus Student 16,547 Points

Hi Steve,

Appreciate the speedy reply! Unfortunately I've tried saving the permalinks but still the problem persists.

My permalink structure is /news/%postname%/ so that my news posts are prepended with /news/ and my CPT's are set to Hierarchical = true (false is default) so that I can have /cemeteries/cemetery-name/ for example. URL is http://www.brentstevensonmemorials.co.uk/cemeteries/ to put it into context.

Not sure if that will help at all.

Thanks Michael

Hi Michael

I've had a little bit of time to look into it better and tried to do it on a website I work on. It could be that you're missing the 'has_archive' parameter in the Custom Post Type UI plugin. This needs to be enabled and then the permalinks saving again.

Hopefully this works.