Comments: Allow WP_Comment_Query fields to project arbitrary columns#203
Closed
dd32 wants to merge 1 commit into
Closed
Comments: Allow WP_Comment_Query fields to project arbitrary columns#203dd32 wants to merge 1 commit into
dd32 wants to merge 1 commit into
Conversation
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the Core Committers: Use this line as a base for the props when committing in SVN: To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
Extends the `fields` argument of `WP_Comment_Query` to accept:
- a column name from the `$wpdb->comments` table (e.g.
`'comment_post_ID'`) — returns a flat array of distinct values for
that column.
- a `'col_a=>col_b'` pair (e.g. `'comment_ID=>comment_post_ID'`) —
returns an associative array keyed by col_a's value with col_b's
value as the entry, mirroring `WP_Query` / `WP_Term_Query`'s
`'id=>parent'` idiom.
The motivation is to avoid hydrating full `WP_Comment` objects when the
caller only needs one column. In one reference case (gp-translation-
helpers), hydrating ~32K comments to extract distinct post IDs took
~6.6s; the equivalent raw SQL took ~610ms (~11x), all of it from
skipping hydration.
Single-column results apply `DISTINCT` so callers can drop the common
`array_unique( wp_list_pluck( ... ) )` wrapping.
Column names must be passed in their exact case; the only exception is
the `ID` suffix of `comment_ID` / `comment_post_ID`, which is accepted
in any case (e.g. `'comment_post_id'`).
`'ids'` and `''` retain their existing meaning; the new behaviour is
purely additive.
Props dd32.
See #65313.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Owner
Author
|
Re-filing on WordPress/wordpress-develop per ticket workflow. See follow-up PR. |
087c972 to
9115f83
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
WP_Comment_Query'sfieldsargument to accept any column of$wpdb->comments(single string) or an array of columns, mirroring the flexibility ofWP_User_Query.DISTINCTso the commonarray_unique( wp_list_pluck( ... ) )pattern collapses to a single'fields' => 'comment_post_ID'query.'ids'and''(default) keep their existing semantics — the change is purely additive.Implements Trac #65313.
Why
Plugins that want "give me the distinct
comment_post_IDs matching X" currently have three options, none good:fields => ''+array_column( $q->comments, 'comment_post_ID' )— hydrates every comment object just to throw all but one field away.fields => 'ids'+get_comment( $id )->comment_post_IDin a loop — N+1 cache lookups.In the gp-translation-helpers reference case, hydrating ~32K comments to extract distinct post IDs took ~6.6s; the equivalent raw SQL returned the same 25,694 IDs in ~610ms (~11× speedup, all of it from skipping hydration).
API
Column names must be passed in exact case. The only exception is the
IDsegment ofcomment_ID/comment_post_ID, which accepts any case (comment_id,comment_Id,comment_ID).Unknown column names fall through silently to the default behaviour (
WP_Commentobjects), to keep the path forward-compatible.Test plan
npm run test:php -- tests/phpunit/tests/comment/query.php— 165 tests / 487 assertions pass (no regressions).npm run test:php -- --group comment— 539 tests / 1365 assertions pass.tests/phpunit/tests/comment/query.phpcover:IDsuffix; strict case for non-IDcolumnsstdClass[]withDISTINCTappliedWP_Commentobjects'ids'semantics unchanged (still returnsint[])array()without a follow-up query🤖 Generated with Claude Code