Skip to content

Link Template: Avoid reading a missing post in wp_get_shortlink() - #14543

Open
bor0 wants to merge 3 commits into
WordPress:trunkfrom
bor0:fix/shortlink-missing-post
Open

bor0 wants to merge 3 commits into
WordPress:trunkfrom
bor0:fix/shortlink-missing-post

Conversation

@bor0

@bor0 bor0 commented Oct 20, 2026 •

Copy link
Copy Markdown
Member

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

In wp_get_shortlink() with the query context, $post_id comes from get_queried_object_id() while $post comes from get_post(). When the ID is non-zero but get_post() returns null, wp_shortlink_header() warns:

Attempt to read property "post_type" on null in wp-includes/link-template.php

Seen on WordPress.org from a scanner POSTing many junk parameters to a handbook landing page. This derives $post_id from the resolved post, matching the post context branch.

🤖 Generated with Claude Code

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) <noreply@anthropic.com>
@github-actions

github-actions Bot commented Oct 20, 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.

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

Props bor0, westonruter.

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


Props Bot commands

You can trigger Props Bot actions by commenting on this PR:

  • @propsbot add <username> to give props to a GitHub or WordPress.org user. Requires write access.
  • @propsbot remove <username> to leave a user out of the props. Requires write access.
  • @propsbot refresh to refresh this list. Adding the props-bot label works too.

@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/link-template.php Outdated
Comment on lines +4175 to +4178
$post = get_post( get_queried_object_id() );
if ( $post ) {
$post_id = $post->ID;
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about this?

Suggested change
$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;
}

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, applied in c5d8fde. I also added tests for the query context, including one where the queried object is not a post (it fails with the original warning on trunk).

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) <noreply@anthropic.com>
@bor0
bor0 requested a review from westonruter October 26, 2026 10:53
@bor0

bor0 commented Nov 26, 2026

Copy link
Copy Markdown
Member Author

heya @westonruter 👋 what is remaining for us to push this through the finish line?

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