I usually work with the ancestors
attribute of the post object:
if ( in_array( $target_id, $post->ancestors ) ) {
// do whatever
}
But while debugging the post object I’ve seen that there is no trace of it.
Can someone help me understand the reason why this happens?
The reason you don’t see it if you dump object is because it’s not actual object property.
WP_Post
implements magical __get()
method for ancestors
and several more keys like that. When you access $post->ancestors()
what you actually get is not some value from the object, but return of get_post_ancestors()
function executed on it.
So in a nutshell this is like a virtual API shortcut.