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

  • Reduce the globals. Use the function 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".
    – bueltge
    Mar 27, 2015 at 11:35
  • Thanks for your help but it doesn't work, it's undefined...
    – freaky
    Mar 27, 2015 at 12:26
  • If is undefined, then was include via wrong hook.
    – bueltge
    Mar 27, 2015 at 15:35
  • So which hook do I need to used to get for example post ID in post type edit page in admin?
    – freaky
    Mar 27, 2015 at 20:57
  • 1
    Now you have changed the question from global $current_screen to global $post but still you don't show us how are you using it. Your code doesn't use global $post anywhere. If you don't describe your problem properly is quite sure that you won't get a good answer.
    – cybmeta
    Mar 28, 2015 at 12:50

1 Answer 1


The basic mistake is, that every data is available at the point where you write your code. That isn't the case and hooks are there to provide access to the core code during different states of the core loading procedure graph.

In short and very simplified things run like this:

# core loads
// ...more stuff...
# point at where all must-use-plugins are loaded - first entry point
do_action( 'muplugins_loaded' );
// ...more stuff...
# point at where all plugins are loaded - second entry point
do_action( 'plugins_loaded' );
// ...more stuff...
# main core stuff available - use this hook instead of `init` for multisite plugins
do_action( 'wp_loaded' );
// ...more stuff...
# themes functions.php files loaded - use this hook for theme stuff
do_action( 'after_setup_theme' );
// ...rendering happens...

Somewhere in between, WP core sets up (fills with data) different globally available datasets in global variables. Some are defined during the rendering flow, others before it, etc. In other words, when you write a plugin and just drop code in the wild (not in a callback attached to a hook or filter), then it's very uncertain where it will appear. In your example possibly before plugins_loaded is called. The result is either a PHP error (undefined variable) or just something empty, because the variable wasn't filled.

Conclusion: Always! put your code in callbacks that are attached to (the appropriate) hooks and filters. In most cases this means a bit of shotgun debugging and attaching your callbacks to different hooks/filters that you find when following the core loading procedure with reading core files.

Short cut to data available in the admin and on which hook it is available via the wecodemore/"Current Admin Info"-Plugin.

  • Thanks a lot for your answer! I need to learn more about it... I will try to make it works, and will take inspiration of your plugin.
    – freaky
    Mar 13, 2015 at 23:36
  • I tried during hours but I still don't understand... I think I miss something... For example I include a file and I just want to retrieve post id for example. But I can't access to it inside the included file...
    – freaky
    Mar 27, 2015 at 1:16
  • 1
    @freaky Please update your question with your current code so I can see what's going wrong.
    – kaiser
    Mar 27, 2015 at 10:31

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.