projectnami
projectnami copied to clipboard
WP_Query row counts can be incorrect if query is filtered (seen in bbPress)
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...
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.