Make WordPress Core

Opened 10 years ago

Closed 9 years ago

#17066 closed defect (bug) (duplicate)

$wp_query->found_posts is set to 0 with posts_per_page=-1

Reported by: patrik bona Owned by:
Milestone: Priority: normal
Severity: normal Version: 3.1
Component: Query Keywords: has-patch
Focuses: Cc:


If I modify/create new WP_Query object with parameter posts_per_page=-1 (or showposts=-1), then $wp_query->found_posts has incorrect value. It always return 0 instead of count of found posts.

If I don't set posts_per_page, or set it to some positive number, then it is set to correct value.

Attachments (2)

17066.diff (1.2 KB) - added by solarissmoke 10 years ago.
pop-found-rows.diff (437 bytes) - added by wonderboymusic 9 years ago.

Download all attachments as: .zip

Change History (14)

#1 @nacin
10 years ago


If $limits is empty, we should do a count() on $this->posts. Unsure whether we should aim to avoid the count when no_found_rows is true, as I imagine that's just looking out for the extra query.

#2 @scribu
10 years ago

I think we should avoid the count: In my mind, "no_found_rows" signals that you're not interested in the total number of posts.

Related: http://core.trac.wordpress.org/ticket/10964#comment:77

10 years ago

#3 @solarissmoke
10 years ago

  • Keywords has-patch added

patch also removes apply_filters_ref_array

#4 @scribu
10 years ago

  • Milestone changed from Awaiting Review to 3.2

#5 @scribu
10 years ago

Related: #17195

#6 @ryan
10 years ago

  • Milestone changed from 3.2 to Future Release

Punted per bug scrub.

#8 @sennza
9 years ago

  • Cc bronson@… added

#9 @wonderboymusic
9 years ago

My reading of $q['no_found_rows'] is "don't do the FOUND_ROWS() query" - doesn't mean that found_rows is forbidden from being set, just don't use the query to do it. The query is also suppressed if there is no limit, which to me means "set found_rows from count( $posts ) since we got all of the posts already"

I made a new patch that IMO is simpler and addresses the original purpose of the ticket which is returning a number that isn't zero in an instance like this:

$q = new WP_Query( array( 'posts_per_page' => -1 ) );
error_log( $q->found_posts );

#10 @wonderboymusic
9 years ago

  • Milestone changed from Future Release to 3.5

Updated patch to populate a property that the object actually contains.

#11 @SergeyBiryukov
9 years ago

Suggest merging with #14426.

#12 @wonderboymusic
9 years ago

  • Milestone 3.5 deleted
  • Resolution set to duplicate
  • Status changed from new to closed

Sergey's patch on #14426 does the trick

Note: See TracTickets for help on using tickets.