WordPress Global $post Object: get_the_ID, setup_postdata, and wp_reset_postdata Explained

The global $post: get_the_ID(), get_queried_object_id() and setup_postdata()

Template tags like the_title(), get_the_ID() and the_content() have no post argument: they read the global $post. Inside the main loop that is the current post. Outside it, in a sidebar widget, a shortcode in a widget, a hook in the header or after another loop, the global may be empty or point to a different post, and the template tags return the wrong data.

Problem: A shortcode in a sidebar widget uses get_the_ID() to load data of the current page, but gets the ID of the last post of a “related posts” list, or 0.

Solution: Use get_queried_object_id() for the page that is being viewed, pass post IDs or objects explicitly to functions that accept them, and when a loop changes the global with the_post() or setup_postdata(), restore it with wp_reset_postdata().

The viewed page vs the current post

<?php
// The page the visitor requested: works anywhere after the query ran
$page_id = get_queried_object_id();

// The post of the current loop iteration: correct only inside a loop
$loop_id = get_the_ID();

On a single post both are the same until some code runs another loop. On an archive, get_queried_object_id() is the term or the author, and get_the_ID() is the post of the current iteration. In hooks that run before the main query (init, plugins_loaded), neither has a value yet; template_redirect and later hooks are safe.

Functions with a post argument

Most template tags have a get_ version that takes a post, and these never depend on the global:

<?php
$post_obj = get_post( 42 );

echo esc_html( get_the_title( $post_obj ) );
echo esc_url( get_permalink( $post_obj ) );
echo get_the_post_thumbnail( $post_obj, 'medium' );
echo esc_html( get_the_date( '', $post_obj ) );

In shortcodes, widgets and REST callbacks, prefer this style: no global state, no reset needed.

When you need the global

Some functions accept no argument, the_content() and the_excerpt() for example. Then set the global and restore it afterwards:

<?php
global $post;

$post = get_post( 42 );   // setup_postdata() alone does not change $post
setup_postdata( $post );

the_title( '<h3>', '</h3>' );
the_excerpt();

wp_reset_postdata();      // back to the post of the main query

setup_postdata() fills the other globals that template tags use ($id, $authordata, $pages), but it does not change $post itself; that is why the assignment is needed.

A custom loop with WP_Query changes the global on every the_post():

<?php
$related = new WP_Query( array( 'posts_per_page' => 3, 'post__not_in' => array( get_queried_object_id() ), 'no_found_rows' => true ) );

while ( $related->have_posts() ) {
    $related->the_post();
    the_title( '<li>', '</li>' );
}
wp_reset_postdata();

Which query function to use for such lists is compared in get_posts(), WP_Query or query_posts().

NOTE: Code that runs after somebody else’s loop inherits their mistakes: if a widget above yours forgot wp_reset_postdata(), get_the_ID() in your code is wrong too. Using get_queried_object_id() and explicit post arguments makes your code independent of that.