Newer/Older posts links display same posts on every page

The question:

I am using a plugin called “Types” to generate custom post types for drink/food recipes. I want to show booth drink and food recipes on the same page. The posts are displaying fine, but for some reason, when you click “newer posts” or “older posts” it will bring you to a new page, but the new page will have the same posts as page 1. Anyone have any ideas? Thanks in advance!

Here’s my loop:

<div class="postContainer">
    <ol class="news postList">
        <?php query_posts( array('post_type' => array( 'recipe', 'drink-recipe' ), 'cat' => array('12','8'), 'showposts' => 5 )); ?>
        <?php if( have_posts() ): ?>
        <?php while( have_posts() ): the_post(); ?>
        <?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' ); ?>
        <li class="row">
            <a href="<?php the_permalink(); ?>" rel="nofollow noreferrer noopener" rel="nofollow noreferrer noopener" title="<?php the_title(); ?>" style="background-image:url('<?php echo $image[0]; ?>')">
                <div class="seven offset-by-five">
                    <h3><?php the_title(); ?></h3>
                    <h4><?php the_time('F j, Y') ?></h4>
                    <?php echo the_excerpt(); ?>
                    <div href="<?php the_permalink(); ?>" rel="nofollow noreferrer noopener" rel="nofollow noreferrer noopener" title="<?php the_title(); ?>" class="button">Read more</div>
                </div>
            </a>
        </li>
        <?php endwhile; ?>
    </ol>
</div>

<div id="pagenav">
    <span class="newer"><?php previous_posts_link(__('Newer Posts »','blog')) ?></span>
    <span class="older"><?php next_posts_link(__('« Older Posts','blog')) ?></span>
</div>   

<?php else: ?>

<div id="post404" class="noposts">

    <p><?php _e('None found.','404'); ?></p>

</div>

<?php endif; wp_reset_query(); ?>

<?php get_footer(); ?>

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

If you look here you’ll see that pagination is a caveat of query_posts, and won’t work without the paged variable

The right way to do

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

and then add:

'paged' => $paged

to your argument array within query_posts.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment