From 20a3c0f939400de56fa288356b8c5d75ccc10ac0 Mon Sep 17 00:00:00 2001 From: Boro Sitnikovski Date: Tue, 20 Oct 2026 12:44:14 +0200 Subject: [PATCH 1/2] Link Template: Avoid reading a missing post in wp_get_shortlink(). When the queried object ID doesn't resolve to a post, `get_post()` returns null and the post type lookup warns. Co-Authored-By: Claude Opus 5.5 (1M context) --- src/wp-includes/link-template.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/wp-includes/link-template.php b/src/wp-includes/link-template.php index 484a8c8f8591c..85fa37ada5efe 100644 --- a/src/wp-includes/link-template.php +++ b/src/wp-includes/link-template.php @@ -4172,8 +4172,10 @@ function wp_get_shortlink( $id = 0, $context = 'post', $allow_slugs = true ) { $post_id = 0; if ( 'query' === $context && is_singular() ) { - $post_id = get_queried_object_id(); - $post = get_post( $post_id ); + $post = get_post( get_queried_object_id() ); + if ( $post ) { + $post_id = $post->ID; + } } elseif ( 'post' === $context ) { $post = get_post( $id ); if ( ! empty( $post->ID ) ) { From c5d8fde3ea55ed83c7a90e5fabffcd7779e3d3b2 Mon Sep 17 00:00:00 2001 From: Boro Sitnikovski Date: Mon, 26 Oct 2026 11:51:41 +0100 Subject: [PATCH 2/2] Link Template: Use the queried object in wp_get_shortlink(). In the `query` context, use the queried object directly when it is a post instead of looking the post up again by ID. Adds tests for the `query` context, including a queried object that is not a post. See #66348. Co-Authored-By: Claude Opus 5.5 (1M context) --- src/wp-includes/link-template.php | 7 ++++--- tests/phpunit/tests/link.php | 26 ++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/src/wp-includes/link-template.php b/src/wp-includes/link-template.php index 85fa37ada5efe..010625e1e2b42 100644 --- a/src/wp-includes/link-template.php +++ b/src/wp-includes/link-template.php @@ -4172,9 +4172,10 @@ function wp_get_shortlink( $id = 0, $context = 'post', $allow_slugs = true ) { $post_id = 0; if ( 'query' === $context && is_singular() ) { - $post = get_post( get_queried_object_id() ); - if ( $post ) { - $post_id = $post->ID; + $queried_object = get_queried_object(); + if ( $queried_object instanceof WP_Post ) { + $post = $queried_object; + $post_id = $queried_object->ID; } } elseif ( 'post' === $context ) { $post = get_post( $id ); diff --git a/tests/phpunit/tests/link.php b/tests/phpunit/tests/link.php index d05a6b210f636..e7ecf70c70149 100644 --- a/tests/phpunit/tests/link.php +++ b/tests/phpunit/tests/link.php @@ -75,6 +75,32 @@ public function test_wp_get_shortlink_with_home_page() { $this->assertSame( home_url( '/' ), wp_get_shortlink( $post_id, 'post' ) ); } + /** + * @ticket 66348 + */ + public function test_wp_get_shortlink_with_query_context() { + $post_id = self::factory()->post->create(); + + $this->go_to( get_permalink( $post_id ) ); + + $this->assertSame( home_url( '?p=' . $post_id ), wp_get_shortlink( 0, 'query' ) ); + } + + /** + * @ticket 66348 + */ + public function test_wp_get_shortlink_with_query_context_and_non_post_queried_object() { + $post_id = self::factory()->post->create(); + + $this->go_to( get_permalink( $post_id ) ); + + // A singular query whose queried object is not a post, with an ID that does not resolve to one. + $GLOBALS['wp_query']->queried_object = (object) array( 'ID' => PHP_INT_MAX ); + $GLOBALS['wp_query']->queried_object_id = PHP_INT_MAX; + + $this->assertSame( '', wp_get_shortlink( 0, 'query' ) ); + } + /** * @ticket 30910 */