Using WordPress ‘get_the_guid()’ PHP function

The get_the_guid() WordPress PHP function retrieves the Post Global Unique Identifier (guid).

Usage

echo get_the_guid($post);

Input:

$post = 42; // Post ID

Output:

https://yourdomain.com/?p=42

Parameters

  • $post (int|WP_Post, Optional) – Post ID or post object. Default is global $post.

More information

See WordPress Developer Resources: get_the_guid()

Examples

Display guid of the current post

Displays the guid of the current post in the loop.

if ( have_posts() ) {
    while ( have_posts() ) {
        the_post();
        echo '<p>Post GUID: ' . get_the_guid() . '</p>';
    }
}

Display guid for a specific post

Displays the guid for a specific post by its ID.

$post_id = 42;
echo '<p>Post GUID: ' . get_the_guid($post_id) . '</p>';

Display guid for all posts in a category

Displays the guid for all posts in a specific category.

$category_id = 5;
$query = new WP_Query( array( 'cat' => $category_id ) );

if ( $query->have_posts() ) {
    while ( $query->have_posts() ) {
        $query->the_post();
        echo '<p>Post GUID: ' . get_the_guid() . '</p>';
    }
    wp_reset_postdata();
}

Display guid for all posts by an author

Displays the guid for all posts by a specific author.

$author_id = 3;
$query = new WP_Query( array( 'author' => $author_id ) );

if ( $query->have_posts() ) {
    while ( $query->have_posts() ) {
        $query->the_post();
        echo '<p>Post GUID: ' . get_the_guid() . '</p>';
    }
    wp_reset_postdata();
}

Display guid for all posts with a specific tag

Displays the guid for all posts with a specific tag.

$tag_slug = 'coding';
$query = new WP_Query( array( 'tag' => $tag_slug ) );

if ( $query->have_posts() ) {
    while ( $query->have_posts() ) {
        $query->the_post();
        echo '<p>Post GUID: ' . get_the_guid() . '</p>';
    }
    wp_reset_postdata();
}