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

Joseph McClellan
Joseph McClellan
4,045 Points

Pagination with WP_Query on wordpress template

Having serious issues with getting a pagination to show up on this page template that's using a custom WP_Query. Any wordpress experts out there that might be able to help me troubleshoot?

<?php
    // paging variable
    $paged = (get_query_var('page')) ? get_query_var('page') : 1;
    // tag variable
    $flr_tag = get_field('posts_tag');
    // the arguments
    $args = array(
        'post_type'         =>  'post',
        'category_name'     =>  'show-highlights',
        'tag'               =>  $flr_tag, 
        'posts_per_page'    =>  5,
        'paged'             =>  $paged,
    ); 
    // assign arguments to new WP_Query
    $my_query = new WP_Query( $args );
    // start the loop
    while( $my_query->have_posts() ) :
           $my_query->the_post();
?>
    <div id="content-<?php the_ID(); ?>" class="entry-content clearfix">                    
        <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
        <div class="post-header-meta">
            <?php if( is_multi_author() ) { ?>
                <span class="the-author"><a href="<?php echo get_author_posts_url( get_the_author_meta( 'ID' ) ); ?>" title="<?php echo get_the_author_meta( 'display_name' ); ?>"><?php echo the_author_meta( 'display_name' ); ?></a>&nbsp;&mdash;&nbsp;</span>
            <?php } // end if ?>
            <?php if( strlen( trim( get_the_title() ) ) == 0 ) { ?>
                <a href="<?php the_permalink(); ?>" rel="bookmark" title="<?php printf( esc_attr__( '%s', 'standard' ), the_title_attribute( 'echo=0' ) ); ?>"><span class="the-time updated"><?php the_time( get_option( 'date_format' ) ); ?></span></a>
            <?php } else { ?>
                <span class="the-time updated"><?php the_time( get_option( 'date_format' ) ); ?></span>
            <?php } // end if/else ?>
        </div><!-- /.post-header-meta -->
        <?php the_excerpt(); ?>
    </div>
<?php endwhile; ?>
<div class="pagenav">
    <div class="alignleft"><?php previous_posts_link('Previous') ?></div>
    <div class="alignright"><?php next_posts_link('Next') ?></div>
</div>
<?php wp_reset_postdata(); // fixes bug where below ACF fields wont display ?>

Gist: https://gist.github.com/jmcclellan/e23577a75125b848ebbb

6 Answers

Joseph McClellan
Joseph McClellan
4,045 Points

Still don't have it fixed, but I think the reason it might not be working is because that WP_Query code I posted is following the regular WP loop code (if have_posts() while have_posts())

Any thoughts?

Zac Gordon
STAFF
Zac Gordon
Treehouse Guest Teacher

Hey Joseph,

That's not the problem, because as long as you have the $my_query-> part infront here "while( $my_query->have_posts() ) : $my_query->the_post();" you're fine.

Try changing $my_query to $wp_query, sometimes that does it.

Joseph McClellan
Joseph McClellan
4,045 Points

I was under the impression that $wp_query is reserved, is that not correct?

Zac Gordon
STAFF
Zac Gordon
Treehouse Guest Teacher

Yes, but for that reason I have heard it working for pagination. Worth a try, but might not solve it :-/

david claytor
david claytor
6,317 Points

For some reason that solved it for me. I searched forever to find the solution. CSS Tricks has a nice write up on it here: http://css-tricks.com/snippets/wordpress/paginate-custom-post-types/

jack hamilton
jack hamilton
5,609 Points

I always struggle with this would love to see a video on it.