Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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 .

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]
share|improve this question
    
1  
Do you ever use "Sticky Posts"? (p.s. You should try to get that accept rate of yours up. It'll encourage more people to answer your questions.) –  mrwweb May 31 '12 at 15:39
    
@mrwweb thank you, 'ignore_sticky_posts' => 1 solves the problem. One thing I don't understand though is that while posts_per_page returns the number of posts that I want, numberposts returns more than I want. So does numberposts automatically include the stickies? –  Ben May 31 '12 at 16:03
add comment

1 Answer

up vote 1 down vote accepted

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.)

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.