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

Bob Sutherton
Bob Sutherton
20,160 Points

Request for help: Custom Post Type Pagination issue

I have been wrestling with this issue for about a month now I guess and haven't been able to crack it. I am fairly new to WordPress and PHP so I am happy to have come this far.

I am building a website for someone else, and I have custom post types that need to paginate. I think I want to do it with the paginate_links() method although there appears to be several different ways one might go about this task.

I have gotten as far as to get the pagination to appear at the bottom of the page, but it only recycles the same page of posts. It just shows the same page over and over again.

So, I am really stuck here. Any help to push me past this hump would be so greatly appreciated! Thank you in advance!

Do you have any code to show ?

Bob Sutherton
Bob Sutherton
20,160 Points
<?php 
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

$args = array (
    'post_type' => 'bhagavan'
);

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

<?php if (have_posts()) : while ($the_query->have_posts()) : $the_query->the_post(); ?>
    <div class="wrapper">
        <div class="excerpt">
            <div id="showcase"><img src="<?php the_field('showcase_image'); ?>" class="feat-img"></div>
                <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
                <p><?php the_excerpt(); ?></p>
        </div>
    </div>

<?php endwhile; ?>

    <div class="paginate">
        <?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
            ) );
        ?>
        <?php wp_reset_postdata(); ?>
    </div>

<?php else: ?>

    <p>There are no posts or pages here.</p>

<?php endif; ?>

2 Answers

Bob Sutherton
Bob Sutherton
20,160 Points

Solved! I'm pretty excited.

So what I was missing was one little line, of course.

in the $args array toward the top I just needed to add one piece:

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

$args = array (
    'post_type' => 'bhagavan',
   **'paged' => $paged** 

);

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

Thanks for your help, Dan. You made me meditate a little more on that link you sent, which ultimately lead me to the solution.

Voilà! :P It's always good to take a step back. Congrats!

From what I know, it would be better for you to create a new loop and call your post type in your new loop.

You should end up with something in those lines:

<?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; $loop = new WP_Query( array( 'post_type' => 'accessories', 'posts_per_page' => 9, 'orderby'=> 'menu_order' 'paged'=>$paged ) ); ?>

There is an interesting thread for you here: https://wordpress.org/support/topic/pagination-with-custom-post-type-listing

Good Luck!

Bob Sutherton
Bob Sutherton
20,160 Points

Thanks Dan, I will look more into what you have said. As for the link you gave, I must have visited that page at least 5 times in my quest to figure this out. It seems that, as I said, there are several different ways this can be done and that way worked for her but, for whatever reason, has not worked for me.