I am wondering whether using global $post
is a 'best practice' when it is necessary to get the $post
within a callback function triggered by an action hook.
The code would be:
class myPlugin {
public function __construct() {
add_action('wp', array( $this, 'init'));
}
public function init() {
global $post;
if (isset($post)) {
// do something
}
}
}
I've tried to:
Call the init function with $post as argument; and
What is posted here
But, none of the options work.
How safe would be to use global
?
Let's say that the 'do something' is a custom WP Query, which may change the value of $post
, having declared it global has the potential of doing something unexpected right?
Is there a better way?
Cheers.