Using WordPress ‘get_queried_object_id()’ PHP function

The get_queried_object_id() WordPress PHP function retrieves the ID of the currently queried object.

Usage

To use the function, simply call it like this:

$queried_object_id = get_queried_object_id();

Parameters

This function has no parameters.

More information

See WordPress Developer Resources: get_queried_object_id

Examples

Display the queried object ID on a single post page

This example displays the queried object ID on a single post page.

if (is_single()) {
    $queried_object_id = get_queried_object_id();
    echo 'The queried object ID is: ' . $queried_object_id;
}

Get the queried object ID for the current archive page

This example retrieves the queried object ID for the current archive page.

if (is_archive()) {
    $queried_object_id = get_queried_object_id();
    echo 'The queried object ID for this archive is: ' . $queried_object_id;
}

Check if the queried object ID is a specific page

This example checks if the queried object ID is equal to a specific page ID.

$specific_page_id = 42;
$queried_object_id = get_queried_object_id();

if ($queried_object_id === $specific_page_id) {
    echo 'This is the specific page with ID: ' . $specific_page_id;
}

Get the queried object ID for the current category page

This example retrieves the queried object ID for the current category page.

if (is_category()) {
    $queried_object_id = get_queried_object_id();
    echo 'The queried object ID for this category is: ' . $queried_object_id;
}

Get the queried object ID for the current tag page

This example retrieves the queried object ID for the current tag page.

if (is_tag()) {
    $queried_object_id = get_queried_object_id();
    echo 'The queried object ID for this tag is: ' . $queried_object_id;
}

Tagged in

Leave a Comment

Your email address will not be published. Required fields are marked *