Random WordPress Function

Learn about a new WordPress function every day!


Function Signature:

get_queried_object

Function Description:

Retrieves the currently queried object.

Function Examples:

⚠️ Examples below are generated with GPT-3 once every hour. Do not take them too seriously.
Consider them as some extra input in your learning process - reason about them. Will it work? What could fail?
// Example 1: Retrieve the current queried object in a custom WordPress template
$queried_object = get_queried_object();
if ( $queried_object ) {
    // Use the queried object data here
} else {
    // Handle the case when there is no queried object
}
// Example 2: Get the post object when viewing a single post
$queried_object = get_queried_object();
if ( $queried_object instanceof WP_Post ) {
    // Access post data like ID, title, content, etc.
} else {
    // Handle the case when the queried object is not a post
}
// Example 3: Retrieve the current category object in a category archive page
$queried_object = get_queried_object();
if ( $queried_object instanceof WP_Term && is_category() ) {
    // Access category data like ID, name, description, etc.
} else {
    // Handle the case when the queried object is not a category or it's not a category archive
}