Make WordPress Core

Opened 4 years ago

Closed 4 years ago

#51376 closed defect (bug) (fixed)

wp_get_shortlink produces PHP Notice on unregistered post type

Reported by: apedog's profile apedog Owned by: sergeybiryukov's profile SergeyBiryukov
Milestone: 5.6 Priority: normal
Severity: normal Version:
Component: Posts, Post Types Keywords: has-patch
Focuses: Cc:

Description

Calling wp_get_shortlink with an existing ID of an unregistered custom post type, the following PHP notice is shown:
Notice: Trying to get property 'public' of non-object in \wp-includes\link-template.php on line 3913

Step to reproduce:

  1. Create a custom post type.
  2. Add new post of custom post type to database.
  3. Unregister/disable custom post type.
  4. Call wp_get_shortlink with ID from step 2

Problem is due to get_post( $id ) returning object regardless if registered or not. So $post_id is not empty.
However get_post_type_object returns null with no sanity checks before $post_type->public is tested.

Attachments (2)

51376.diff (928 bytes) - added by apedog 4 years ago.
51376.alt.diff (874 bytes) - added by apedog 4 years ago.
51376 - alternative patch

Download all attachments as: .zip

Change History (6)

@apedog
4 years ago

#1 @apedog
4 years ago

  • Keywords has-patch added

@apedog
4 years ago

51376 - alternative patch

#2 @sabernhardt
4 years ago

  • Component changed from General to Posts, Post Types

#3 @SergeyBiryukov
4 years ago

  • Milestone changed from Awaiting Review to 5.6
  • Owner set to SergeyBiryukov
  • Status changed from new to reviewing

#4 @SergeyBiryukov
4 years ago

  • Resolution set to fixed
  • Status changed from reviewing to closed

In 49067:

Posts, Post Types: Avoid a PHP notice in wp_get_shortlink() if the post type is no longer registered.

Props apedog.
Fixes #51376.

Note: See TracTickets for help on using tickets.