Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Post ID in Wordpress Admin

Tags:

php

wordpress

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' );
like image 361
Victor Avatar asked Dec 11 '11 09:12

Victor


People also ask

How do I find my WordPress post ID?

How to Find a Post ID in WordPress. For me, the quickest and easiest way to find a post ID is done from the All Posts screen — found by logging into the WordPress dashboard and clicking Posts > All Posts. From here, simply hover your mouse over the post you want to find the ID for.

How do I print a WordPress post ID?

The get_the_ID() and the_ID() functions display the current post's ID. The main difference between the two functions is that get_the_ID() returns the post ID without displaying it, while the_ID() always prints the post ID. The get_page_by_title() function works for WordPress pages and custom post types.

How can I get post ID from post slug?

If you want to get post id by slug in WordPress, you can do so using a function that passes the slug as a parameter and returns the post ID or Page ID. This may sound like a complicated WordPress function but it is very straightforward and easy to implement in your theme or a custom plugin.


2 Answers

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

like image 169
Ciprian Tepes Avatar answered Sep 20 '22 14:09

Ciprian Tepes


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.

like image 24
Caleb Avatar answered Sep 22 '22 14:09

Caleb