The get_queried_object() WordPress PHP function retrieves the currently queried object, which is the main subject of the webpage.
Usage
$current_object = get_queried_object();
Parameters
- None
More information
See WordPress Developer Resources: get_queried_object()
Examples
Get the current category on a category archive page
Code:
if (is_category()) { $current_category = get_queried_object(); echo 'Current category: ' . $current_category->name; }
Explanation: This code checks if the current page is a category archive page, and if so, it retrieves the current category object and displays its name.
Get the current author on an author archive page
Code:
if (is_author()) { $current_author = get_queried_object(); echo 'Current author: ' . $current_author->display_name; }
Explanation: This code checks if the current page is an author archive page, and if so, it retrieves the current author object and displays their display name.
Get the current tag on a tag archive page
Code:
if (is_tag()) { $current_tag = get_queried_object(); echo 'Current tag: ' . $current_tag->name; }
Explanation: This code checks if the current page is a tag archive page, and if so, it retrieves the current tag object and displays its name.
Get the current custom post type on a post type archive page
Code:
if (is_post_type_archive()) { $current_post_type = get_queried_object(); echo 'Current post type: ' . $current_post_type->label; }
Explanation: This code checks if the current page is a post type archive page, and if so, it retrieves the current post type object and displays its label.
Get the current post or page on a singular page
Code:
if (is_singular()) { $current_post = get_queried_object(); echo 'Current post or page: ' . $current_post->post_title; }
Explanation: This code checks if the current page is a singular page (a single post, a single page, or a post in a custom post type), and if so, it retrieves the current post object and displays its title.