I tried to get $post var outside function php in wordpress admin panel. The only way that I find to access to $post is to add_action.
However I want to get this var outside the add_action and global var seems to doesn't work in add_action.
How can I get $post in admin panel outside function.php.
I search during hours an can't find a solution...
EDIT: Example
require_once(TP_PLUGIN_PATH . 'includes/my-file-framework.php' ); // it'a a metabox framework
require_once(TP_PLUGIN_PATH . 'includes/my-file-config.php' ); // I load the file that include metabox array to build metabox
class The_plugin_Admin {
public function __construct() {
add_action( 'pre_get_posts', array($this, 'the_metabox' ));
}
public function the_metabox() {
apply_filters( 'the_metabox_setup','');
}
}
new The_plugin_Admin;
And my-file-config.php where I wan to retrieve Wordpress global:
add_filter( 'the_metabox_setup', 'the_metabox_setup_callback' );
function the_metabox_setup_callback() {
global $post;
print_r($post); //empty
$post_ID = isset($_GET['post']) ? $_GET['post'] : 0;
echo $post_ID; // empty on new post because not already set
$post_types = get_post_types(array('public' => true), 'names', 'and'); // no get all post type on the first time, need to loop again to get all
$an_array[] = array(......);
$an_array[] = array(......);
$an_array[] = array(......);
$an_array[] = array(......);
foreach ($an_array as $array) {
new The_Metabox($array); // contruct my metabox, the class was declared previous in my-file-framework.php
}
}
get_current_screen()
. You find inside the array of the result of this function the page, the post type and more. Small hint, easy to find with plugin "Debug Objects".global $current_screen
toglobal $post
but still you don't show us how are you using it. Your code doesn't useglobal $post
anywhere. If you don't describe your problem properly is quite sure that you won't get a good answer.