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: |
|
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)
Change History (14)
comment:2
scribu
— 2 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
solarissmoke
— 2 years ago
comment:3
solarissmoke
— 2 years ago
- Keywords has-patch added
patch also removes apply_filters_ref_array
comment:7
SergeyBiryukov
— 12 months ago
comment:9
wonderboymusic
— 10 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();
wonderboymusic
— 9 months ago
comment:10
wonderboymusic
— 9 months ago
- Milestone changed from Future Release to 3.5
Updated patch to populate a property that the object actually contains.
comment:11
SergeyBiryukov
— 9 months ago
Suggest merging with #14426.
comment:12
wonderboymusic
— 9 months ago
- Milestone 3.5 deleted
- Resolution set to duplicate
- Status changed from new to closed
Sergey's patch on #14426 does the trick
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.