get_queried_object()WP 3.1.0

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:

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.

1 time — 0.000026 sec (very fast) | 50000 times — 0.06 sec (speed of light)

No Hooks.

Returns

WP_Term|WP_Post_Type|WP_Post|WP_User|null.

Usage

$queried_object = get_queried_object();

Examples

1

#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
)
*/
1

#2 Object of the author

Now, let's call the function on the author's posts page and see what object is returned:

WP_User Object
(
	[data] => stdClass Object
		(
			[ID] => 1
			[user_login] => kama
			[user_pass] => $P$B/spPGtZwp7IPd/FuTORIpu95BZyqW/
			[user_nicename] => kama
			[user_email] => [email protected]
			[user_url] =>
			[user_registered] => 2011-04-05 15:22:38
			[user_activation_key] =>
			[user_status] => 0
			[display_name] => kama
		)

	[ID] => 1
	[caps] => Array
		(
			[administrator] => 1
		)

	[cap_key] => wp_capabilities
	[roles] => Array
		(
			[0] => administrator
		)

	[allcaps] => Array
		(
			[switch_themes] => 1
			[edit_themes] => 1
			[activate_plugins] => 1
			[edit_plugins] => 1
			[edit_users] => 1
			[edit_files] => 1
			[manage_options] => 1
			[moderate_comments] => 1
			[manage_categories] => 1
			[manage_links] => 1
			[upload_files] => 1
			[import] => 1
			[unfiltered_html] => 1
			[edit_posts] => 1
			[edit_others_posts] => 1
			[edit_published_posts] => 1
			[publish_posts] => 1
			[edit_pages] => 1
			[read] => 1
			[level_10] => 1
			[level_9] => 1
			[level_8] => 1
			[level_7] => 1
			[level_6] => 1
			[level_5] => 1
			[level_4] => 1
			[level_3] => 1
			[level_2] => 1
			[level_1] => 1
			[level_0] => 1
			[edit_others_pages] => 1
			[edit_published_pages] => 1
			[publish_pages] => 1
			[delete_pages] => 1
			[delete_others_pages] => 1
			[delete_published_pages] => 1
			[delete_posts] => 1
			[delete_others_posts] => 1
			[delete_published_posts] => 1
			[delete_private_posts] => 1
			[edit_private_posts] => 1
			[read_private_posts] => 1
			[delete_private_pages] => 1
			[edit_private_pages] => 1
			[read_private_pages] => 1
			[delete_users] => 1
			[create_users] => 1
			[unfiltered_upload] => 1
			[edit_dashboard] => 1
			[update_plugins] => 1
			[delete_plugins] => 1
			[install_plugins] => 1
			[update_themes] => 1
			[install_themes] => 1
			[update_core] => 1
			[list_users] => 1
			[remove_users] => 1
			[add_users] => 1
			[promote_users] => 1
			[edit_theme_options] => 1
			[delete_themes] => 1
			[export] => 1
			[administrator] => 1
		)

	[filter] =>
)
0

#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
)
*/
0

#4 Archive object of the post type

Let's call the function on the custom taxonomy archive page:

stdClass Object
(
	[labels] => stdClass Object
		(
			[name] => Radios
			[singular_name] => Radio
			[add_new] => Add radio
			[add_new_item] => Adding a radio
			[edit_item] => Edit radio
			[new_item] => Radio
			[view_item] => View radio page
			[search_items] => Search radio
			[not_found] => Radio not found
			[not_found_in_trash] => No radio found
			[parent_item_colon] =>
			[all_items] => Radio
			[archives] => Radio
			[insert_into_item] => Insert in post
			[uploaded_to_this_item] => Uploaded for this post
			[featured_image] => Miniature posts
			[set_featured_image] => Set thumbnail
			[remove_featured_image] => Remove thumbnail
			[use_featured_image] => Use as a thumbnail
			[filter_items_list] => Filter list of posts
			[items_list_navigation] => Navigating through the list of posts
			[items_list] => List of posts
			[menu_name] => Radio
			[name_admin_bar] => Radio
		)

	[description] =>
	[public] => 1
	[hierarchical] =>
	[exclude_from_search] =>
	[publicly_queryable] => 1
	[show_ui] => 1
	[show_in_menu] => 1
	[show_in_nav_menus] => 1
	[show_in_admin_bar] => 1
	[menu_position] => 5
	[menu_icon] => dashicons-format-audio
	[capability_type] => post
	[map_meta_cap] => 1
	[register_meta_box_cb] =>
	[taxonomies] => Array
		(
			[0] => radio_cat
		)

	[has_archive] => 1
	[rewrite] => Array
		(
			[slug] => radio
			[with_front] => 1
			[pages] => 1
			[feeds] => 1
			[ep_mask] => 1
		)

	[query_var] => radio
	[can_export] => 1
	[delete_with_user] =>
	[_builtin] =>
	[_edit_link] => post.php?post=%d
	[name] => radio
	[cap] => stdClass Object
		(
			[edit_post] => edit_post
			[read_post] => read_post
			[delete_post] => delete_post
			[edit_posts] => edit_posts
			[edit_others_posts] => edit_others_posts
			[publish_posts] => publish_posts
			[read_private_posts] => read_private_posts
			[read] => read
			[delete_posts] => delete_posts
			[delete_private_posts] => delete_private_posts
			[delete_published_posts] => delete_published_posts
			[delete_others_posts] => delete_others_posts
			[edit_private_posts] => edit_private_posts
			[edit_published_posts] => edit_published_posts
			[create_posts] => edit_posts
		)

	[label] => Radio stations
)
0

#5 Get post ID on the single post page

$post_id = get_queried_object()->ID;

Notes

  • Global. WP_Query. $wp_query WordPress Query object.

Changelog

Since 3.1.0 Introduced.

get_queried_object() code WP 7.0

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