How to get the queried object ID in WordPress using get_queried_object_id

The get_queried_object_id function in WordPress is a part of the WordPress query API. It is used to retrieve the ID of the currently queried object in the WordPress Loop. This function is often used in conditional tags to determine the type of page that is currently being viewed, such as whether it’s a single post, a page, a category, a tag, or an author.

The function can be used in a variety of contexts. For instance, it can be used to get the ID of a post or page, the ID of a category or tag, or the ID of an author. This makes it possible to fetch specific data related to the currently queried object, such as the title of a post or page, the name of a category or tag, or the name of an author.

By using the get_queried_object_id function, developers can write more dynamic and adaptable code that responds to the specific context in which it is running. This can lead to more efficient and effective WordPress themes and plugins, as well as a better overall user experience.

Parameters

The get_queried_object_id function in WordPress does not accept any parameters.

Return Value

This function returns an integer that represents the ID of the queried object.

Examples

How to get the ID of the queried object in a single post or page

if (is_singular()) {
 $post_id = get_queried_object_id();
 echo "<p>The ID of the current post or page is: " . $post_id . "</p>";
}

This code snippet checks if the currently queried object is a single post or page using the is_singular() function. If it is, it uses the get_queried_object_id() function to get the ID of the queried object (the single post or page) and then echoes it out in a paragraph.

How to get the ID of the queried object in a category archive page

if (is_category()) {
 $cat_id = get_queried_object_id();
 echo "<p>The ID of the current category is: " . $cat_id . "</p>";
}

This code snippet checks if the currently queried object is a category archive page using the is_category() function. If it is, it uses the get_queried_object_id() function to get the ID of the queried object (the category) and then echoes it out in a paragraph.

How to get the ID of the queried object in a tag archive page

if (is_tag()) {
 $tag_id = get_queried_object_id();
 echo "<p>The ID of the current tag is: " . $tag_id . "</p>";
}

This code snippet checks if the currently queried object is a tag archive page using the is_tag() function. If it is, it uses the get_queried_object_id() function to get the ID of the queried object (the tag) and then echoes it out in a paragraph.

Conclusion

The get_queried_object_id function in WordPress is a tool that retrieves the ID of the queried object. This function is typically used in scenarios where the developer needs to identify the current object being queried on a page, such as a specific post, page, category, or author. By using this function, developers can obtain the necessary ID to further manipulate or retrieve data related to the queried object in WordPress.

Related WordPress Functions