Pagination in custom loop not working

I have already searched several topics and questions regarding this problem, but I haven´t found any answer that suits my code.

The pagination for my custom posts is displayed, but when I click in Show next posts >> the same posts from the previous page are shown, even though the URL shows ?paged=2.

Read More

My code is the following:

<div class="podcast-entries">
    <?php
    $args = array( 'post_type' => 'strn5_podcasts',
                   'posts_per_page' => 3,
                   'paged' => (get_query_var('paged') ? get_query_var('paged') : 1 );
    $temp = $wp_query;
    $wp_query = null;
    $wp_query = new WP_Query( $args );

    if ( $wp_query->have_posts() ) : 
    while ( $wp_query->have_posts() ) :
    $wp_query->the_post();
    ?>

        <div <?php post_class(); ?> >

            <h4 class="podcast-title"><?php the_title(); ?></h4>

            <div class="podcast-content">
                <?php the_content(); ?>
            </div>

            <h6><?php the_time('F j, Y'); ?></h6>

            <div class="post-separator col-lg-12"></div>

        </div>

    <?php endwhile; endif; ?>

    <div class="navigation-links">

        <div class="next-post">
            <?php next_posts_link( '>> Siguiente Entrada' ); ?>
        </div>

        <div class="previous-post">
            <?php previous_posts_link( 'Anterior Entrada >>' ); ?>
        </div>

    </div>

    <?php $wp_query = null; 
    $wp_query = $temp;
    wp_reset_query(); ?>

</div> <!-- .podcast-entries -->

Related posts

Leave a Reply

2 comments

  1. Usually what I do when something doesn’t work is that I strip it from everything and start rebuilding it with bare basics to see what the problem might be.

    Why don’t you try a simpler form of loop to see if you can get it to prin.t

    <?php query_posts(array('posts_per_page' => 3, 'post_type' => 'strn5_podcasts', 'paged' => get_query_var('page'))); if (have_posts()) : while (have_posts()) : the_post(); ?>
      <p><?php the_title(); ?></p>
    <?php endwhile; endif;?>
    
    <div class="navigation-links">
      <div class="next-post"><?php next_posts_link( '>> Siguiente Entrada' ); ?></div>
      <div class="previous-post"><?php previous_posts_link( 'Anterior Entrada >>' ); ?></div>
    </div>
    

    It’s pretty much the same thing you are doing (almost) but without any variables or any other thing that “could” go wrong.

    Edit

    In the end it turned out that the paged parameter can be used with paged or page and in this case it worked out for you with page instead of paged

    So it ended up looking like so:

    'paged' => get_query_var('page')

    On a final note, using get_query_var('page') is needed if you’re using query posts in a custom page template that you’ve set as your homepage.

  2. I agree with sulfureous. I think more specifically though, these three lines are the problem:

    $temp = $wp_query;
    $wp_query = null;
    $wp_query = new WP_Query( $args );
    

    Based off of your description it seems like this would cause the issue if $wp_query is being caught as not null. I’m not sure why you’d even have that in there but you must have your reason.

    At the very least take it out the first two lines of that to help you troubleshoot.