get_queried_object(): WP_Term|WP_Post_Type|WP_Post|WP_User|null

Retrieves the currently queried object.

Description

Wrapper for WP_Query::get_queried_object().

Return

WP_Term|WP_Post_Type|WP_Post|WP_User|null The queried object.

Source

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

Changelog

VersionDescription
3.1.0Introduced.

User Contributed Notes

  1. Skip to note 2 content

    The “currently-queried object” means the object that is the subject of the webpage:

    – On a category archive, tag archive, or other taxonomy archive page, it will return the WP_Term object of the current category, tag, or other term.
    – If you have set a posts page where your basic posts are displayed, get_queried_object() will return the WP_Post object of that page.
    – On post type archive pages, it will return the WP_Post_Type object of the given post type.
    – On an author archive page, it will return the WP_User object of that author.
    – On any singular page (a single post, a single page, or a post in a custom post type), it will return the WP_Post object of that post or page.

    Be careful not to use get_queried_object() and get_post() or global $post interchangeably. On a singular post, those will all return the same thing. But, for example, if you have a page called “Blog” that displays your posts, get_queried_object() will return the “Blog” page whereas get_post() will return the current post in the loop.

You must log in before being able to contribute a note or feedback.