• Resolved Brian Becker

    (@brian-becker)


    $count = 3;
    $q = new WP_Query( array(
                'orderby'          => 'date',
                'posts_per_page'   => "{$count}"
            ) );
            if ( $q->have_posts() ) : while ( $q->have_posts() ) : $q->the_post();
            // spit out the stuff
            endwhile; endif; wp_reset_postdata();

    — the above provides 7 posts…not 3

    $count = 3;
    $q = new WP_Query( array(
                'orderby'          => 'date',
                'posts_per_page'   => "{$count}"
            ) );
            $counter = 0;
            if ( $q->have_posts() ) : while ( $q->have_posts() && $counter++ < $count ) : $q->the_post();
            // spit out the stuff
            endwhile; endif; wp_reset_postdata();

    Of course, this limits it to 3 posts…

    But WHY won’t the first work and show just 3?

Viewing 4 replies - 1 through 4 (of 4 total)
  • What’s up with this line?
    'posts_per_page' => "{$count}"

    Shouldn’t it be
    'posts_per_page' => $count

    Thread Starter Brian Becker

    (@brian-becker)

    Same result using => $count … I’ve also tried
    ‘posts_per_page’ => 3

    And no difference in results…7 posts shown rather than 3.

    So strange.

    Thread Starter Brian Becker

    (@brian-becker)

    Problem solved – sticky posts… you have to actually set the following to keep sticky from overriding your request…which I think is crazy that it overrides posts_per_page setting…

    But now that I know, I won’t get caught by this bug again.

    “ignore_sticky_posts” => true

    I’ve been stuck for some hours trying to figure out a similar issue.
    Thank you sooo much for posting your solution!!!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘WP_Query giving too many posts’ is closed to new replies.