I'm having trouble understanding the behavior of a WP_Post variable (and / or possibly the behavior of var_dump itself).

As far as I know, var_dump will inspect all data from a variable. From the PHP documentation:

This function displays structured information about one or more expressions that includes its type and value. Arrays and objects are explored recursively with values indented to show structure.

Then, why is it that var_dump($post) doesn't show metadata, whereas var_dump($post->metadata) does?

I've already read the offficial documentation, specially this particular comment, as well as this answer. I assume the explanation is there, in plain sight -- however, it's still unclear to me.

What's going on here? Is it some basic PHP behavior that I'm not aware? (I'm not sure if this question is because a lack of understanding how WordPress works or how PHP works. Sorry if this is the wrong site to ask).


1 Answer 1


There is no 'magic' in it, but there are two magic methods of WP_Post class, __isset(), and __get().

Your extra_data is not a property of WP_Post class, so first var_dump does not include it.

A reference to non existing property of WP_Post employs those magic methods, mentioned above, to retrieve post's metadata.

First, $post->__isset('extra_data') will be executed, if false is returned, then $post->extra_data will be an empty array, otherwise $post->__get('extra_data') will run, returning metadata. That's why your second var_dump shows extra_data.

Note: Milo's first comment to this question is, in fact, the perfect answer.

  • The core of my question is why doesn't var_dump include it? Isn't the very purpose of var_dump to dump everything from a variable? Dec 18, 2018 at 15:26
  • 1
    The second paragraph of my answer explains it. extra_data is not a property of WP_Post, and direct reference to it does not make it as such. It simply attempts to retrieve metadata with a key equals to 'extra_data'. Dec 18, 2018 at 17:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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