Opened 13 years ago
Closed 12 years ago
#17066 closed defect (bug) (duplicate)
$wp_query->found_posts is set to 0 with posts_per_page=-1
Reported by: | anonymized_5610363 | Owned by: | |
---|---|---|---|
Milestone: | Priority: | normal | |
Severity: | normal | Version: | 3.1 |
Component: | Query | Keywords: | has-patch |
Focuses: | Cc: |
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)
#2
@
13 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
#9
@
12 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 ); exit();
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.