WordPress.org

Make WordPress Core

Opened 2 years ago

Closed 9 months 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:
Priority: normal Milestone:
Component: Query Version: 3.1
Severity: normal Keywords: has-patch
Cc: bronson@…

Description

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 2 years ago.
pop-found-rows.diff (437 bytes) - added by wonderboymusic 9 months ago.

Download all attachments as: .zip

Change History (14)

comment:1 nacin2 years ago

http://core.trac.wordpress.org/browser/trunk/wp-includes/query.php?rev=17610#L2633

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.

comment:2 scribu2 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

solarissmoke2 years ago

comment:3 solarissmoke2 years ago

  • Keywords has-patch added

patch also removes apply_filters_ref_array

comment:4 scribu2 years ago

  • Milestone changed from Awaiting Review to 3.2

comment:5 scribu2 years ago

Related: #17195

comment:6 ryan2 years ago

  • Milestone changed from 3.2 to Future Release

Punted per bug scrub.

comment:8 sennza12 months ago

  • Cc bronson@… added

comment:9 wonderboymusic10 months 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 );
exit();

comment:10 wonderboymusic9 months ago

  • Milestone changed from Future Release to 3.5

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

comment:11 SergeyBiryukov9 months ago

Suggest merging with #14426.

comment:12 wonderboymusic9 months 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.