projectnami icon indicating copy to clipboard operation
projectnami copied to clipboard

WP_Query row counts can be incorrect if query is filtered (seen in bbPress)

Open rmc47 opened this issue 5 years ago • 1 comments

Currently, https://github.com/ProjectNami/projectnami/blob/72b69a2e2f5840b2cb77c83e7316fdb6efa9e570/wp-includes/class-wp-query.php#L2854 performs a COUNT() query to find the number of rows that would be returned if $limits did not apply.

However, if the posts_request filter causes the query to be changed, this row count may be invalid. This is the case in bbPress, where the _bbp_has_replies_where filter alters the WHERE clause to include the original topic as well as its replies in a single query.

Instead, we could perform the row count on the query after any filters have been applied if possible.

Incoming PR to do that, but it's a little scary...

rmc47 avatar Mar 29 '19 12:03 rmc47

FYI, we are reviewing this. I keep thinking there is another case where the COUNT() is executed later in the code, perhaps in WPDB. I believe the preferred situation would be to execute it as close to the submission of the query to the DB as possible.

patrickebates avatar Apr 05 '19 02:04 patrickebates