wp_query (and post_id) is empty in admin_init

In the admin, there is not such thing as the current WP_Query, because most of the pages on the admin are not linked to any post, so the pages that have any relation to a post you should grab the ID from the $_GET like that:

add_action('admin_init','do_optional_featured_article');
function do_optional_featured_article() {
    if( isset($_GET['post']) ) {
        $post_id = absint($_GET['post']); // Always sanitize
        $post = get_post( $post_id ); // Post Object, like in the Theme loop
        echo "<pre>" . esc_html( print_r( $post, true ) ) . "</pre>"; // Better way to print_r without breaking your code with the html...
        die();
    }
}

If you are trying to achieve this on a Saving action, the post id should be in $_POST['post_ID'];

Hope I’ve helped.


So I’ve changed your code just a little bit more:

function get_admin_post() {
    $post_id = absint( isset($_GET['post']) ? $_GET['post'] : ( isset($_POST['post_ID']) ? $_POST['post_ID'] : 0 ) );
    $post = $post_id != 0 ? get_post( $post_id ) : false; // Post Object, like in the Theme loop
    return $post;
}

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)