From 880e15c67332039cd614b0b938d9cef7cced7be0 Mon Sep 17 00:00:00 2001 From: Dhrupo Nil Date: Thu, 16 Jul 2026 09:46:47 +0600 Subject: [PATCH] Comments: Exclude notes from get_page_of_comment(). `get_page_of_comment()` defaults to the 'all' comment type, which `WP_Comment_Query` does not exclude the internal 'note' comment type from. As a result, a note dated before a given comment inflates the calculated page number, so `get_comment_link()` can produce a `cpage` that points to the wrong comments page. The public comment list excludes notes, so the pagination calculation must exclude them too. Pass `type__not_in => array( 'note' )` to the count query so notes never affect the page a regular comment appears on. This mirrors the approach taken for the comments list table in [61525] / #64474. Adds a regression test. Fixes #65642. --- src/wp-includes/comment.php | 17 +++++----- .../tests/comment/getPageOfComment.php | 32 +++++++++++++++++++ 2 files changed, 41 insertions(+), 8 deletions(-) diff --git a/src/wp-includes/comment.php b/src/wp-includes/comment.php index 20303dc1e7e17..c66930f559f82 100644 --- a/src/wp-includes/comment.php +++ b/src/wp-includes/comment.php @@ -1100,14 +1100,15 @@ function get_page_of_comment( $comment_id, $args = array() ) { } $comment_args = array( - 'type' => $args['type'], - 'post_id' => $comment->comment_post_ID, - 'fields' => 'ids', - 'count' => true, - 'status' => 'approve', - 'orderby' => 'none', - 'parent' => 0, - 'date_query' => array( + 'type' => $args['type'], + 'type__not_in' => array( 'note' ), + 'post_id' => $comment->comment_post_ID, + 'fields' => 'ids', + 'count' => true, + 'status' => 'approve', + 'orderby' => 'none', + 'parent' => 0, + 'date_query' => array( array( 'column' => "$wpdb->comments.comment_date_gmt", 'before' => $comment->comment_date_gmt, diff --git a/tests/phpunit/tests/comment/getPageOfComment.php b/tests/phpunit/tests/comment/getPageOfComment.php index 44e6af5ac3f87..3655780cbcfa2 100644 --- a/tests/phpunit/tests/comment/getPageOfComment.php +++ b/tests/phpunit/tests/comment/getPageOfComment.php @@ -36,6 +36,38 @@ public function test_last_comment() { $this->assertSame( 1, get_page_of_comment( $comment_first[0], array( 'per_page' => 10 ) ) ); } + /** + * Ensures that internal 'note' comments are excluded from the page calculation. + * + * Notes share the comments table but are never displayed in the public comment + * list, so they must not shift the page a regular comment appears on. + * + * @ticket 65642 + * + * @covers ::get_page_of_comment + */ + public function test_notes_should_not_affect_page() { + $p = self::factory()->post->create(); + + self::factory()->comment->create_post_comments( $p, 1, array( 'comment_date' => '2013-09-14 00:00:00' ) ); + + // An internal note dated between the two regular comments must be ignored. + self::factory()->comment->create( + array( + 'comment_post_ID' => $p, + 'comment_type' => 'note', + 'comment_approved' => '1', + 'comment_parent' => 0, + 'comment_date' => '2013-09-15 00:00:00', + ) + ); + + $comment_target = self::factory()->comment->create_post_comments( $p, 1, array( 'comment_date' => '2013-09-16 00:00:00' ) ); + + // Only one regular comment precedes the target, so it is on page 2. + $this->assertSame( 2, get_page_of_comment( $comment_target[0], array( 'per_page' => 1 ) ) ); + } + public function test_type_pings() { $p = self::factory()->post->create(); $now = time();