WordPress.org

Make WordPress Core

Opened 3 years ago

Closed 3 years ago

Last modified 3 years ago

#39164 closed defect (bug) (invalid)

WP 4.7 broke get_post_type or requires an explicit integer value?

Reported by: ssuess Owned by:
Milestone: Priority: normal
Severity: normal Version: 4.7
Component: General Keywords:
Focuses: Cc:
PR Number:

Description

I just upgraded to WP 4.7, and suddenly code that uses get_post_type($id) stopped returning anything, and didn't throw an error either.

After trying a few things, I found that it would start working again if I changed my code from

get_post_type($id)

to

get_post_type(intval($id))

But I can't find anything in the docs about WP suddenly requiring explicit integer values. Anyone else seeing this?

Change History (3)

#1 @ssuess
3 years ago

UPDATE

So, using trim instead of intval works too.

get_post_type(trim($id))

And checking $id

(preg_match('/\s/',$id))

shows that it had a space. But oddly, this worked just fine in WP 4.6, so something must have changed to make that less forgiving in WP 4.7

#2 @dd32
3 years ago

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

This was an intentional change to get_post() in 4.7 - passing an invalid parameter may have previously returned a result, but it could've been an incorrect resource, for example, a value being cast to a numeric 1 and fetching from the incorrect post.

get_post( 123 ) is the same as get_post( '123' ) but not the same as get_post( " 123 " ) (Which now fails) IMHO, so I agree with the change, especially in this case.

For reference, see #37738

#3 @ssuess
3 years ago

Makes sense, I was just surprised it worked previously. I didn't even know my variable had a space in it until 4.7 :)

Note: See TracTickets for help on using tickets.