Skip to content

REST API: Avoid undefined property warning when creating a post with a slug#12404

Open
abhishekrijal wants to merge 1 commit into
WordPress:trunkfrom
abhishekrijal:fix/rest-create-item-undefined-id
Open

REST API: Avoid undefined property warning when creating a post with a slug#12404
abhishekrijal wants to merge 1 commit into
WordPress:trunkfrom
abhishekrijal:fix/rest-create-item-undefined-id

Conversation

@abhishekrijal

@abhishekrijal abhishekrijal commented Jul 4, 2026

Copy link
Copy Markdown

Trac ticket

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

Problem

WP_REST_Posts_Controller::create_item() passes $prepared_post->id (lowercase) to
wp_unique_post_slug():

$prepared_post->post_name = wp_unique_post_slug(
    $prepared_post->post_name,
    $prepared_post->id, // lowercase — never set on this object
    'publish',
    $prepared_post->post_type,
    $prepared_post->post_parent
);

$prepared_post is a fresh stdClass from prepare_item_for_database(), which only ever assigns
$prepared_post->ID (uppercase), and only on the update path. On create, neither id nor ID is
set, so $prepared_post->id is an undefined property. Every other reference to the id in this file
already uses ->ID.

  • PHP < 8: silent E_NOTICE, coerced to null0 (the correct value for a new post).
  • PHP 8.0+: E_WARNING "Undefined property: stdClass::$id" on every matching request.

This fires on POST /wp/v2/posts when the request includes a slug and a draft/pending status
(common from the block editor), and is noisy in error loggers such as Sentry.

Fix

Pass the post ID defensively — 0 for a brand-new post — matching the ! empty( $prepared_post->ID )
guard already used elsewhere in the controller:

! empty( $prepared_post->ID ) ? $prepared_post->ID : 0,

Testing

Adds test_create_draft_with_slug_does_not_emit_undefined_property_warning() to
tests/phpunit/tests/rest-api/rest-posts-controller.php, which creates a draft with an explicit slug
and asserts (a) a 201 with the expected unique slug and (b) no Undefined property: stdClass::$id
warning is emitted during dispatch.

…th a slug.

`WP_REST_Posts_Controller::create_item()` passed `$prepared_post->id` (lowercase) to
`wp_unique_post_slug()`. That property is never set on the stdClass built by
`prepare_item_for_database()`, which only assigns the uppercase `ID`, and only on the
update path. On create, `$prepared_post->id` is therefore always undefined, so on PHP 8+
creating a draft/pending post with a slug emitted
"Warning: Undefined property: stdClass::$id".

Pass the post ID defensively (0 for a new post), matching the `! empty( $prepared_post->ID )`
guard used elsewhere in the controller, and add a regression test.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@github-actions

github-actions Bot commented Jul 4, 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 aveeshek.

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

@github-actions

github-actions Bot commented Jul 4, 2026

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.

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.

1 participant