How do I get the current edit page ID in the admin?

Most solutions I found are for front-end use. This is for a plugin, so the all activity is in the back-end.

How do I get the page ID that’s being currently use(edited) in the admin?

Note I’m out of the loop. I just need to get the ID of page(not posts) that I’m currently seeing in the back-end.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

You can also use

$post_id = $_GET['post'];

Or you can use a hook (probably better).

function id_WPSE_114111() {
    global $post;
    $id = $post->ID;
    // do something
}

add_action( 'admin_notices', 'id_WPSE_114111' );

You will need to add a conditional since this will run on all admin pages, I recommend using get_current_screen();

For example to run only on pages:

function id_WPSE_114111() {

    global $my_admin_page;
    $screen = get_current_screen();

    if ( is_admin() && ($screen->id == 'page') ) {
        global $post;
        $id = $post->ID;
        var_dump($id);
    }
}

add_action( 'admin_notices', 'id_WPSE_114111' );

Method 2

You can add this code in functions.php file and it will give you a meta box above the publish settings box when editing a post or page.

 <?php

function cf_post_id() {
    global $post;

   // Get the data
   $id = $post->ID;

   // Echo out the field
   echo '<input type="text" name="_id" value="' . $id . '" class="widefat" disabled />';
  }

 function ve_custom_meta_boxes() {
    add_meta_box('projects_refid', 'Post ID', 'cf_post_id', 'post', 'side', 'high');
    add_meta_box('projects_refid', 'Page ID', 'cf_post_id', 'page', 'side', 'high');
   }
   add_action('add_meta_boxes', 've_custom_meta_boxes');

?>

Method 3

This is probably the “most secure” way of checking what the current post is on the post edit (and add) page in wp admin.

function wpse114111_get_current_post_id(): ?WP_Post {
    global $post;

        if (empty($post) && array_key_exists('post', $_GET)) {
            $post = get_post($_GET['post']);
        }

        // Optional: get an empty post object from the post_type
        if (empty($post) && array_key_exists('post_type', $_GET)) {
            $object = new stdClass();
            $object->post_type = $_GET['post_type'];
            return new WP_Post($object);
        }

        if (empty($post)) {
            return null;
        }

        return $post;
}

Method 4

You just use this code in the functions.php file, It will give you a current edit page or post ID

function get_the_current_page_id(){
$post_id = null;
if( isset( $_REQUEST['post']) || isset($_REQUEST['post_ID'] ) ){
    $post_id = empty( $_REQUEST['post_ID'] ) ? $_REQUEST['post'] : $_REQUEST['post_ID' ];
}
    
//print_r($post_id) For check
//die();
}

//get_the_current_page_id() //Call the function


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x