[wp-trac] [WordPress Trac] #8071: Refrain from querying all the comments on a post when paged
WordPress Trac
noreply at wordpress.org
Sat Sep 12 05:52:24 UTC 2015
#8071: Refrain from querying all the comments on a post when paged
------------------------------------+-----------------------------
Reporter: markjaquith | Owner: wonderboymusic
Type: enhancement | Status: assigned
Priority: normal | Milestone: 4.4
Component: Comments | Version: 2.7
Severity: normal | Resolution:
Keywords: has-patch dev-feedback | Focuses:
------------------------------------+-----------------------------
Changes (by wonderboymusic):
* keywords: needs-patch => has-patch dev-feedback
Comment:
[attachment:8071.diff 8071.diff] makes this work
Create 2 helper functions (these can be renamed, whatever):
* `wp_comments_per_page()` - figures out what the default per page value
is
* `wp_comment_pages_count( $total, $top_level )` - total can be read from
`$post->comment_count`, `$top_level` is the count of a queried array of
top level comment ids for a post that runs when comments are threaded
Add 2 query vars to `WP_Comment_Query`:
* `parent__in` - for querying for comment_IDs that belong to a set of
parents
* `parent__not_in` for completeness
Add a new `orderby` value in `WP_Comment_Query`: `comment__in` - when
querying by `comment__in`, need to return them in the same order.
In `comment_template()`:
- We're going to split the query - add `'fields' => 'ids'` to
`$comment_args`
- If comments are threaded, query for all of the top level comment ids -
else, get all of the comment IDs
- Figure out how many pages of comments there are using
`wp_comment_pages_count()`
- Save the number of pages on `$wp_query` as `->comment_pages_count`
- If we determine that there is more than 1 page of comments:
- save the current page on `$wp_query` as `->comment_pages_page`
- slice the list of comment IDs based on `page` and `per_page`
- if comments are threaded, use `parent__in` in
`wp_threaded_comment_ids()` to query for comment children (1 query for
comment IDs per depth level)
- set `comment__in` to the comment IDs, orderby `comment__in`, get all of
the comments in 1 query
In `wp_list_comments()`:
* If no comments are passed to the function (means we're in the loop) and
`$wp_query->comment_pages_page` is set, reset `$r['page']` to 1, so that
the Walker doesn't try to create the offset that we created by slicing the
comment_IDs in `comments_template()`
Any Comments? (Huzzah!)
--
Ticket URL: <https://core.trac.wordpress.org/ticket/8071#comment:14>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list