WordPress.org

Make WordPress Core

Opened 3 years ago

Closed 2 weeks ago

Last modified 2 weeks ago

#17034 closed defect (bug) (fixed)

E_NOTICE with WP_DEBUG when calling get_the_ID()

Reported by: tillkruess Owned by: wonderboymusic
Milestone: 4.0 Priority: normal
Severity: minor Version: 3.1
Component: Posts, Post Types Keywords:
Focuses: Cc:

Description

When get_the_ID() is called, but $post is NULL (for example on a 404 page) a E_NOTICE is caused when WP_DEBUG is set to TRUE.

Attachments (2)

28572.diff (456 bytes) - added by UmeshSingla 3 weeks ago.
Fixes get_the_ID() if get_post() returns null
17034.diff (456 bytes) - added by UmeshSingla 3 weeks ago.
Fixes get_the_ID() if get_post() returns null

Download all attachments as: .zip

Change History (14)

comment:1 tillkruess3 years ago

  • Cc me@… added

comment:2 scribu3 years ago

  • Milestone Awaiting Review deleted
  • Resolution set to wontfix
  • Status changed from new to closed

get_the_ID() should only be called from inside The Loop.

Last edited 3 years ago by scribu (previous) (diff)

comment:3 follow-up: jtsternberg7 months ago

Curious, why should get_the_ID() only be called from inside The Loop? Seems if we addressed the notice issue, it would be a decent way to check if there is a post object available.

comment:4 in reply to: ↑ 3 ; follow-up: SergeyBiryukov7 months ago

Replying to jtsternberg:

Seems if we addressed the notice issue, it would be a decent way to check if there is a post object available.

get_post() appears to be the correct way to check for that.

comment:5 SergeyBiryukov3 weeks ago

#28572 was marked as a duplicate.

comment:6 SergeyBiryukov3 weeks ago

  • Component changed from General to Posts, Post Types

comment:7 in reply to: ↑ 4 ; follow-up: UmeshSingla3 weeks ago

Replying to SergeyBiryukov

get_post() appears to be the correct way to check for that.

get_post() can't be used as well, it returns the ID for the latest post on wp-signup.php, I didn't get the logic for that.

comment:8 in reply to: ↑ 7 ; follow-up: SergeyBiryukov3 weeks ago

  • Milestone set to 4.0
  • Resolution wontfix deleted
  • Status changed from closed to reopened

Replying to UmeshSingla:

get_post() can't be used as well, it returns the ID for the latest post on wp-signup.php, I didn't get the logic for that.

That sounds like a separate issue.

We could probably reconsider this to avoid the notice. Your patch on #28572 seems good.

comment:9 in reply to: ↑ 8 ; follow-up: UmeshSingla3 weeks ago

Replying to SergeyBiryukov:

That sounds like a separate issue.

We could probably reconsider this to avoid the notice. Your patch on #28572 seems good.

Yes, I'll try too replicate the get_post() issue, If it confirms, I'll go for a separate ticket and for this ticket do i need to submit the patch here again?

Last edited 3 weeks ago by UmeshSingla (previous) (diff)

comment:10 in reply to: ↑ 9 SergeyBiryukov3 weeks ago

Replying to UmeshSingla:

do i need to submit the patch here again?

Yes, please.

UmeshSingla3 weeks ago

Fixes get_the_ID() if get_post() returns null

UmeshSingla3 weeks ago

Fixes get_the_ID() if get_post() returns null

comment:11 wonderboymusic2 weeks ago

  • Owner set to wonderboymusic
  • Resolution set to fixed
  • Status changed from reopened to closed

In 28844:

Check for the existence of $post before using it in get_the_ID(). Return false if it doesn't exist.

Props UmeshSingla.
Fixes #17034.

comment:12 johnjamesjacoby2 weeks ago

Similar enough to #22413 to link here.

Note: See TracTickets for help on using tickets.