get_queried_object()
Sets and Gets the current queried object (complete information about the post, tags, categories, etc.).
This function retrieves different data for different pages (for the current query). So, if we are on:
- post page, it will return the current post object WP_Post of the page we are on.
- category page, it will return the WP_Term object of the current category.
- author page, it will return the WP_User object with user information.
- post type page, it will return the WP_Post_Type object.
This function is a wrapper for the method WP_Query::get_queried_object().
Will return nothing on the page:
The earliest possible moment to call this function
Knowing the first available moment to call is important because if you call this function before this moment, your call may break the correct functioning of the code, as this function sets an important property $wp_query->queried_object! Therefore, it should be called for the first time by WordPress at the moment when the data of the current page are defined.
Calling this function will set the property $wp_query->queried_object, if it has not been set yet.
The property $wp_query->queried_object is reset with each call to WP_Query::query(), i.e., with any request new WP_Query() or get_posts(). A subsequent call to this function resets $wp_query->queried_object (usually to the values that were before, if the WP_Query variables related to the main query have not been changed).
The first moment of the call differs for different pages (tested on WP 5.6.1). Let's consider in detail how it is formed:
-
For pages of type: is_category, is_tag, is_tax, is_post_type_archive, is_author, is_posts_page (front_page) — after setting the query variables on the parse_query hook or later.
Query variables are set by a chain of calls to the following functions:
- WP::main()
- WP::query_posts()
- WP_Query::query()
- WP_Query::get_posts()
- WP_Query::parse_query() — this is the parse_query hook or the first occurrence of the pre_get_posts hook. After that, the pre_handle_404 hook is called, then wp, and so on.
- WP_Query::get_posts()
- WP_Query::query()
- WP::query_posts()
- WP::main()
-
For pages of type: is_singular — after the entire query (after the complete execution of the WP_Query::get_posts() method).
The earliest moment here is the pre_handle_404 filter or the wp event. The first occurrences of the pre_get_posts event or the posts_results, the_posts filters are not suitable (it's still early).
This happens because, to determine the current post page, a query must first be made, and only after it and all hooks related to it, the current post object
WP_Query::$postis set - it is set at the very end of the WP_Query::get_posts() method.
Note that get_queried_object(), get_post(), or global $post may return different data. For example, if you have a "Blog" page that displays other posts, then get_queried_object() will return the "Blog" page, while get_post() will return the current post from the loop. That is, the value of this function remains unchanged regardless of what content is displayed on the page - it always contains data of the current page, unlike get_post() or global $post.
No Hooks.
Returns
WP_Term|WP_Post_Type|WP_Post|WP_User|null.
Usage
$queried_object = get_queried_object();
Examples
#1 Taxonomy object (tags)
Let's call the function on the tags page and see what it returns:
$queried_object = get_queried_object(); print_r( $queried_object ); /* will return: stdClass Object ( [term_id] => 452 [name] => Tag [slug] => metka [term_group] => 0 [term_taxonomy_id] => 452 [taxonomy] => post_tag [description] => [parent] => 0 [count] => 14 [filter] => raw ) */
#2 Object of the author
Now, let's call the function on the author's posts page and see what object is returned:
#3 The object of the post
Let's call the function on the post page - is_singular() and see the result:
$queried_object = get_queried_object(); print_r( $queried_object ); /* will return: WP_Post Object ( [ID] => 2762 [post_author] => 1 [post_date] => 2012-09-27 23:25:59 [post_date_gmt] => 2012-09-27 23:25:59 [post_content] => Post text. [post_title] => Post Title [post_excerpt] => [post_status] => publish [comment_status] => open [ping_status] => closed [post_password] => [post_name] => zagolovok-posta [to_ping] => [pinged] => [post_modified] => 2013-06-10 10:58:25 [post_modified_gmt] => 2013-06-10 10:58:25 [post_content_filtered] => [post_parent] => 0 [guid] => http://example.com/zagolovok-posta [menu_order] => 0 [post_type] => post [post_mime_type] => [comment_count] => 0 [filter] => raw ) */
#4 Archive object of the post type
Let's call the function on the custom taxonomy archive page:
#5 Get post ID on the single post page
$post_id = get_queried_object()->ID;
Notes
- Global. WP_Query.
$wp_queryWordPress Query object.
Changelog
| Since 3.1.0 | Introduced. |
get_queried_object() get queried object code WP 7.0
function get_queried_object() {
global $wp_query;
return $wp_query->get_queried_object();
}