From 153a0983b5348ce35b37561f014207464d69aa6f Mon Sep 17 00:00:00 2001 From: sawfly Date: Tue, 1 Dec 2026 19:03:57 +0000 Subject: [PATCH] paged query var does not supported by singular pages and posts --- src/wp-includes/canonical.php | 3 +- tests/phpunit/tests/canonical/paged.php | 119 ++++++++++++++++++++++++ 2 files changed, 121 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/canonical.php b/src/wp-includes/canonical.php index 5ec43f99c498c..a098496785a63 100644 --- a/src/wp-includes/canonical.php +++ b/src/wp-includes/canonical.php @@ -504,7 +504,8 @@ function redirect_canonical( $requested_url = null, $do_redirect = true ) { $redirect['query'] = remove_query_arg( 'paged', $redirect['query'] ); if ( ! is_feed() ) { - if ( ! is_single() ) { + // Singular posts and pages do not support the 'paged' query var. + if ( ! is_singular() ) { $addl_path = ! empty( $addl_path ) ? trailingslashit( $addl_path ) : ''; if ( $paged > 1 ) { diff --git a/tests/phpunit/tests/canonical/paged.php b/tests/phpunit/tests/canonical/paged.php index d2440ef18bf48..cb1535f9c5c20 100644 --- a/tests/phpunit/tests/canonical/paged.php +++ b/tests/phpunit/tests/canonical/paged.php @@ -26,4 +26,123 @@ public function test_redirect_canonical_with_nextpage_pagination() { // Non-existing page should redirect to the permalink. $this->assertCanonical( $link . '4/', $link ); } + + /** + * Tests that paged requests for pages redirect to the page permalink. + * + * Pages do not support the `paged` query var, so `/page/{n}/` appended + * to a page URL should not serve the page content under another URL. + * + * @ticket 28081 + * + * @dataProvider data_redirect_canonical_paged_requests_for_pages + * + * @param string $test_url Test URL. + * @param string $expected Expected URL after the canonical redirect. + */ + public function test_redirect_canonical_paged_requests_for_pages( $test_url, $expected ) { + self::factory()->post->create( + array( + 'post_type' => 'page', + 'post_name' => 'paged-page', + 'post_title' => 'Paged Page', + ) + ); + + $this->assertCanonical( $test_url, $expected ); + } + + /** + * Data provider. + * + * @return array[] + */ + public function data_redirect_canonical_paged_requests_for_pages() { + return array( + 'pretty paged URL, page 2' => array( '/paged-page/page/2/', '/paged-page/' ), + 'pretty paged URL, large page' => array( '/paged-page/page/2323/', '/paged-page/' ), + 'pretty paged URL, page 1' => array( '/paged-page/page/1/', '/paged-page/' ), + 'paged query string' => array( '/paged-page/?paged=2', '/paged-page/' ), + 'paged query string, extra arg' => array( '/paged-page/page/2/?foo=bar', '/paged-page/?foo=bar' ), + ); + } + + /** + * Tests that paged requests for a child page redirect to the child page permalink. + * + * @ticket 28081 + */ + public function test_redirect_canonical_paged_request_for_child_page() { + $parent_id = self::factory()->post->create( + array( + 'post_type' => 'page', + 'post_name' => 'paged-parent', + ) + ); + self::factory()->post->create( + array( + 'post_type' => 'page', + 'post_name' => 'paged-child', + 'post_parent' => $parent_id, + ) + ); + + $this->assertCanonical( '/paged-parent/paged-child/page/3/', '/paged-parent/paged-child/' ); + } + + /** + * Tests that comment pagination is preserved when the `paged` query var is stripped from a page. + * + * @ticket 28081 + */ + public function test_redirect_canonical_paged_request_for_page_keeps_comment_pagination() { + update_option( 'page_comments', 1 ); + update_option( 'default_comments_page', 'oldest' ); + + self::factory()->post->create( + array( + 'post_type' => 'page', + 'post_name' => 'paged-comments-page', + ) + ); + + $this->assertCanonical( + '/paged-comments-page/comment-page-2/?paged=2', + '/paged-comments-page/comment-page-2/' + ); + } + + /** + * Tests that paged requests for single posts still redirect to the post permalink. + * + * @ticket 28081 + */ + public function test_redirect_canonical_paged_request_for_single_post() { + $post_id = self::factory()->post->create( array( 'post_name' => 'paged-single-post' ) ); + + $link = parse_url( get_permalink( $post_id ), PHP_URL_PATH ); + + $this->assertCanonical( $link . 'page/2/', $link ); + } + + /** + * Tests that paged requests for the posts page are left alone. + * + * @ticket 28081 + */ + public function test_redirect_canonical_paged_request_for_posts_page_is_not_redirected() { + $page_id = self::factory()->post->create( + array( + 'post_type' => 'page', + 'post_name' => 'paged-blog', + ) + ); + self::factory()->post->create_many( 3 ); + + update_option( 'show_on_front', 'page' ); + update_option( 'page_for_posts', $page_id ); + update_option( 'posts_per_page', 1 ); + + $this->assertCanonical( '/paged-blog/page/2/', '/paged-blog/page/2/' ); + } }