Earliest WordPress Hook Where Global $post Variable Exists

In the file wp-includes/class-wp.php there is a hook run which is simply named 'wp'.

This, as far as I can tell, seems to be the earliest hook in which the $post object variable exists. The hook is passed the &$this object which the comments state:

references the WP object. It allows for accessing the properties and methods to further manipulate the object.

Unlike some other hooks which may run more than one time during a single page load (I’m looking at you 'post_results' and 'get_posts') this is run within WP::main() and is called only once.

That’s a nice catch.

Also note the & in the argument being passed from the hook. That means your hook can alter any of the variables in the argument as it’s passed by reference. Such great power!

I recently used this in a plugin to populate some class variables based on the current post being loaded and found other hooks triggering too late in the load process.

Leave a Reply

Your email address will not be published. Required fields are marked *