1

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?

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

  • What is the value in $id here? I tried passing both string and float values and they both worked fine for me. – Matthew Boynes Dec 7 '16 at 21:51
  • I am having some terminology query issues as well. 4.7 has broke the categories on my site, and I need to figure out why and how to fix. – cj5 Dec 7 '16 at 22:00
2

According to the devs at WP (https://core.trac.wordpress.org/ticket/39164):

"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."

So I would consider this the definitive answer. The behavior has changed to something better, it was just a surprise that it worked before.

1

As far as I can tell this is untrue. Let's fall down the rabbit hole...

First we call get_post_type()
Which calls get_post() which if we're not passing some sort of Object
Calls WP_Post::get_instance() which does a conditional check to ensure whatever passed
is_numeric() which will return true for string "numbers".
Finally, it converts the passed number to an integer and returns it up the stack.

All of the above has shipped with WordPress 4.7 so whatever the issue is may be deeper than the built-in get_post_type() since it will accept both integers and "string integers".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.