get_queried_object()WP 3.1.0

Retrieve the currently-queried object.

Wrapper for WP_Query::get_queried_object().

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

No Hooks.

Return

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

Usage

get_queried_object();

Examples

0

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

#2 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

#3 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

#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 6.3

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