Limiting the number of posts in WP_Query leads to unexpected result

The following is supposed to display 10 posts.

Sometimes it displays 11, sometimes 12, and sometimes the desired 10 posts. It behaves the same if I swap posts_per_page for showposts or numberposts .

Read More

Where do I need to search for the problem?

<ul>
    <? wp_reset_query(); ?> 
    <?  $args = array(
        'orderby'           => 'rand',
        'posts_per_page'    => '10',
    );
    $query_footerFavorite = new WP_Query($args);

    while ($query_footerFavorite->have_posts()) : $query_footerFavorite->the_post(); ?>

        <li>
            <a href="<? the_permalink(); ?>" title="<? the_title_attribute(); ?>">
                <? the_title(); ?>
            </a>
        </li>

    <? endwhile; ?>
    <? wp_reset_query(); ?>                 
</ul>

Edit: It seams there is a problem with the posts_per_page, see print_r($query_footerFavorite):

WP_Query Object ( [query_vars] => Array ( [orderby] => rand [posts_per_page] => 10 [error]

Related posts

Leave a Reply

1 comment

  1. The expected behavior of posts_per_page is to NOT count sticky posts. This can be resolved with 'ignore_sticky_posts' => 1, but then the posts don’t stick to the top of the menu. (In the trac ticket for sticky posts this issue is discussed.)

    There are some workarounds like this one, but they all feel a bit hacky to me. Personally I try to just deal with the expected behavior and make sure to style the sticky post class.

    showposts is deprecated since WP2.1 and numberposts seems to only be a valid parameter for get_posts(). (Although I admittedly am unsure why. There’s no mention of that issue on the Codex.)