posts_per_page => 1 shows 2 posts

I am trying to add a button to take the viewer to a random post. Below’s the code I am using:-

    <?php query_posts(array('orderby' => 'rand', 'posts_per_page' => 1));
    if (have_posts()) :
    while (have_posts()) : the_post(); ?>
        <div class="post-lot">
        <a href="https://wordpress.stackexchange.com/questions/303129/<?php the_permalink() ?>">Yes, Robo! Take me there.</a>
        </div>
    <?php endwhile;
    endif; ?>    

But, it keeps showing two links. If I change posts_per_page to 2, it shows 3 links. I tried adding wp_reset_query but the issue still persists.

1 Answer
1

Most probably you have a sticky post there. If you want exact number of posts you need to ignore sticky posts. So your query should look like this:

query_posts( array(
    'orderby' => 'rand',
    'posts_per_page' => 1,
    'ignore_sticky_posts' => 1,
) );

or as suggested, never use query_posts()

$query = new WP_Query( array(
    'orderby' => 'rand',
    'posts_per_page' => 1,
    'ignore_sticky_posts' => 1,
) );

Leave a Comment