How to get Post ID in Wordpress Admin

2019-03-12 21:15发布

I'm developing a Wordpress plugin and I need to get the current Post ID
in the Write Post / Write Page editing screen (outside the loop).

I also need to do it before the "admin_print_scripts" hook, since I would like to pass some data to a javascript file.

I can't use:

$id = $_GET['post'];

because the url doesn't include this variable when you are adding a new post or page.

So far I've tried these options but none of them worked:

A) This returns an ID of 0

function myplugin_setup() {
    global $wp_query;
    $id = $wp_query->get_queried_object_id();
    var_dump($id);
}

add_action('admin_init', 'myplugin_setup' );  

B) This returns an ID of null

function myplugin_setup() {
    global $wp_query;
    $id = $wp_query->post->ID;
    var_dump($id);
}

add_action('admin_init', 'myplugin_setup' );

C) This also returns an ID of null

function myplugin_setup() {
    global $post;
    $id = $post->ID;
    var_dump($id);
}

add_action('admin_init', 'myplugin_setup' );

7条回答
姐就是有狂的资本
2楼-- · 2019-03-12 21:52

The 'admin_init' action is triggered before any other hook when a user accesses the admin area. It is triggered before the new post gets an id.

To get the new post id you can use 'save_post', which is an action triggered whenever a post or page is created or updated (http://codex.wordpress.org/Plugin_API/Action_Reference/save_post).

First you can include your scripts using 'admin_enqueue_scripts', then use 'save_post' to get the new post id. The 'admin_print_scripts' is triggered after 'save_post' and you can use wp_localize_script (https://codex.wordpress.org/Function_Reference/wp_localize_script), or other method to pass the new post id to your javascript.

I needed something similar, but it was used in a class.

class Foo {
    // this will hold the id of the new post
    private $postId = null;

    public function __construct()
    {
        // the actions are triggered in this order
        add_action('admin_enqueue_scripts', array($this, 'EnqueueScripts'));
        add_action('save_post', array($this, 'SavePost'));
        add_action('admin_print_scripts', array($this, 'LocalizeScripts'));
    }

    // enqueue your scripts and set the last parameter($in_footer) to true
    public function EnqueueScripts()
    {
        wp_enqueue_script('myJs', 'js/my.js', array('jquery'), false, true); 
    }

    // use wp_localize_script to pass to your script the post id
    public function LocalizeScripts()
    {
        wp_localize_script('myJs', 'myJsObject', array('postId'=>$this->GetPostId()));
    }

    // if $post_id is different from global post id you are in the write/create post page, else you are saving an existing post
    public function SavePost($post_id)
    {
        if($post_id != $this->GetPostId())
        {
            $this->SetPostId($post_id);
        }
    }

    private function GetPostId()
    {
        if (!$this->postId) {
            global $post;
            if($post)
            {
                $this->SetPostId($post->ID);
            }
        }

        return $this->postId;
    }

    private function SetPostId($postId)
    {
        $this->postId = $postId;
    }
}

Now, in your javascript you can use:

myJsObject.postId
查看更多
倾城 Initia
3楼-- · 2019-03-12 21:54

Make sure you call the global $post after the WordPress query. If you add an action to init or admin_init, the query isn't ready so there's nothing you can get out of the global $post variable.

My advice would be to check the action reference from this page: http://codex.wordpress.org/Plugin_API/Action_Reference and choose one that works for you.

For example, I did this:

add_action( 'admin_head', 'check_page_template' );
function check_page_template() {
    global $post;
    if ( 'page-homepage.php' == get_post_meta( $post->ID, '_wp_page_template', true ) ) {
        // The current page has the foobar template assigned
        // do something

    }
}

And I was able to get the page ID in WP admin

查看更多
SAY GOODBYE
4楼-- · 2019-03-12 22:03

Problem is, you are using admin_init hook. If you look into Action Reference - http://codex.wordpress.org/Plugin_API/Action_Reference - you will see, that this hook is actually called BEFORE quering posts, that's why those variables you use are not yet filled.

You can use some later action (with some is_admin() check), or you may use admin init hook to add action to some later hook, so again it will be used only on admin.

查看更多
闹够了就滚
5楼-- · 2019-03-12 22:11

Use:

global $post

at the beginning of your function. You should then have access to $post->ID to get the id of the current post. This will work for new and existing posts.

查看更多
Anthone
6楼-- · 2019-03-12 22:11

If it is a new post/page, I guess the id does not exists yet, because the post has not been published/added to DB. If you are trying to edit a post/page, I think you may use $id = $_GET['post'];

查看更多
Fickle 薄情
7楼-- · 2019-03-12 22:14

For anyone still wondering the correct hook to call, or one of the many in the wordpress admin lifecycle, is: admin_head

as follows:

function myplugin_setup() {
    global $post;
    $id = $post->ID;
    var_dump($id);
}

add_action('admin_head', 'myplugin_setup' );
查看更多
登录 后发表回答