Skip to content

Comments: Add filter for comment types excluded from queries by default#12310

Open
adamsilverstein wants to merge 8 commits into
WordPress:trunkfrom
adamsilverstein:feature/65537-excluded-comment-types-filter
Open

Comments: Add filter for comment types excluded from queries by default#12310
adamsilverstein wants to merge 8 commits into
WordPress:trunkfrom
adamsilverstein:feature/65537-excluded-comment-types-filter

Conversation

@adamsilverstein

Copy link
Copy Markdown
Member

Description

WP_Comment_Query excludes the note comment type (introduced in 6.9 for the editor Notes feature) from results by default, but the exclusion list is hard-coded with no extension point:

if (
    ! in_array( 'all', $raw_types['IN'], true ) &&
    ! in_array( 'note', $raw_types['IN'], true ) &&
    ! in_array( 'note', $raw_types['NOT IN'], true )
) {
    $raw_types['NOT IN'][] = 'note';
}

A plugin that introduces its own "private" comment type (one that should never appear in standard comment listings, counts, or feeds) has to rewrite the SQL through comments_clauses in every context and re-verify that work on each release. This is the "whack-a-mole" problem reported by the Alpaca issue-tracker plugin, which maintains a dedicated library to do exactly this.

Approach

Introduce a default_excluded_comment_types filter that lets extenders contribute additional comment types to the default-excluded set, generalizing the existing hard-coded note handling:

$excluded_types = apply_filters_ref_array( 'default_excluded_comment_types', array( array( 'note' ), &$this ) );

foreach ( array_unique( (array) $excluded_types ) as $excluded_type ) {
    if (
        ! in_array( 'all', $raw_types['IN'], true ) &&
        ! in_array( $excluded_type, $raw_types['IN'], true ) &&
        ! in_array( $excluded_type, $raw_types['NOT IN'], true )
    ) {
        $raw_types['NOT IN'][] = $excluded_type;
    }
}

Because WP_Comment_Query backs the majority of comment outputs (admin list table, get_comments(), REST collection queries, feeds), a single registration point here removes most of the per-context filtering a plugin currently needs.

Example usage:

add_filter( 'default_excluded_comment_types', function ( $types ) {
    $types[] = 'my_private_type';
    return $types;
} );

Scope / non-goals

This is deliberately a small step, not the full custom-comment-types API tracked in #35214. It adds no registration object, labels, capabilities, or admin UI. It only generalizes the existing default-exclusion behavior so private types can opt in without rewriting query SQL.

Backward compatibility

The default value array( 'note' ) preserves current behavior exactly. No change for sites that don't use the filter.

Testing

Adds full coverage to tests/phpunit/tests/comment/query.php:

  • A custom type added via the filter is excluded by default (alongside note).
  • Returning an empty list makes note appear again, proving the default is filterable.
  • An excluded type is still returned when explicitly requested via type, type__in, or type => 'all' (data provider).
  • The filter receives the default array( 'note' ) and the WP_Comment_Query instance.
  • A filtered type is added to the query SQL only once when also passed in type__not_in.
$ phpunit tests/phpunit/tests/comment/query.php
OK (164 tests, 482 assertions)

$ phpunit --group comment
OK (555 tests, 1381 assertions)

Trac ticket: https://core.trac.wordpress.org/ticket/65537

WP_Comment_Query hard-codes the exclusion of the 'note' comment type
(introduced in 6.9) with no extension point. Plugins that add their own
"private" comment types must instead rewrite query SQL through
comments_clauses in every context and re-verify it on each release.

Introduce a default_excluded_comment_types filter so extenders can
contribute additional comment types to the default-excluded set,
generalizing the existing 'note' handling. The default value of
array( 'note' ) preserves current behavior exactly.

See #65537.
@github-actions

github-actions Bot commented Jun 24, 2026

Copy link
Copy Markdown

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 props-bot label.

Core Committers: Use this line as a base for the props when committing in SVN:

Props adamsilverstein, swissspidy.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@github-actions

Copy link
Copy Markdown

Hi there! 👋

Thank you for your contribution to WordPress! 💖

It looks like this is your first pull request to wordpress-develop. Here are a few things to be aware of that may help you out!

No one monitors this repository for new pull requests. Pull requests must be attached to a Trac ticket to be considered for inclusion in WordPress Core. To attach a pull request to a Trac ticket, please include the ticket's full URL in your pull request description.

Pull requests are never merged on GitHub. The WordPress codebase continues to be managed through the SVN repository that this GitHub repository mirrors. Please feel free to open pull requests to work on any contribution you are making.

More information about how GitHub pull requests can be used to contribute to WordPress can be found in the Core Handbook.

Please include automated tests. Including tests in your pull request is one way to help your patch be considered faster. To learn about WordPress' test suites, visit the Automated Testing page in the handbook.

If you have not had a chance, please review the Contribute with Code page in the WordPress Core Handbook.

The Developer Hub also documents the various coding standards that are followed:

Thank you,
The WordPress Project

@github-actions

Copy link
Copy Markdown

Test using WordPress Playground

The changes in this pull request can previewed and tested using a WordPress Playground instance.

WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser.

Some things to be aware of

  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

Comment thread src/wp-includes/class-wp-comment-query.php Outdated
Comment thread src/wp-includes/class-wp-comment-query.php Outdated
Comment thread tests/phpunit/tests/comment/query.php Outdated
Comment thread tests/phpunit/tests/comment/query.php Outdated
…cess control.

The filter docblock described excluded types as "private", which could
be read as a security boundary. The exclusion only governs default
visibility; excluded types remain retrievable via explicit 'type' or
'all' requests, so document that callers must enforce capability checks
wherever comment data is displayed or exposed.
wp_update_comment_count_now() recalculated a post's stored comment_count
with a hard-coded 'AND comment_type != note', bypassing the
default_excluded_comment_types filter that hides those types from
WP_Comment_Query. A type that opted out of listings still inflated
comment_count and therefore get_comments_number().

Apply the same filtered exclusion set to the counter so the listings and
the stored count stay consistent, removing the need for plugins to also
re-implement the count via pre_wp_update_comment_count_now.

See #35214, #65537.
get_pending_comments_num() recomputed the admin pending-comments count with
a hard-coded 'AND comment_type != note', the same gap fixed in
wp_update_comment_count_now(): a type that opts out of default listings via
default_excluded_comment_types still inflated the pending bubble shown next
to each post in the admin list tables.

Derive the excluded set from the same filter so the pending count stays
consistent with the listings and the stored comment count.

See #35214, #65537.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants