get_queried_object_id()WP 3.1.0

Gets the ID of the current query object. It can be: any post, category, tag, other taxonomy element, author's archive.

Works based on global $wp_query.

Use get_queried_object() when you want to get all of the current query data, not just the query object ID.

1 time — 0.000011 sec (very fast) | 50000 times — 0.02 sec (speed of light) | PHP 7.0.8, WP 4.6

No Hooks.

Return

Int. ID of the queried object.

  • Post ID ― If we're on the post page.
  • Term ID ― If the page of a taxonomy element (category, tag).
  • Author ID ― If on the author's page.
  • 0 ― If the query object does not provide an ID.

Usage

get_queried_object_id();

Examples

0

#1 Get the ID of the current request

$query_id = get_queried_object_id(); // 123

Notes

  • Global. WP_Query. $wp_query WordPress Query object.

Changelog

Since 3.1.0 Introduced.

Code of get_queried_object_id() WP 6.0.1

function get_queried_object_id() {
	global $wp_query;
	return $wp_query->get_queried_object_id();
}