Getting the current queried object in WordPress with get_queried_object

The get_queried_object function in WordPress returns the currently queried object. This can be useful for accessing information about the current page, post, or taxonomy being displayed on the website. It allows developers to retrieve details such as the ID, title, and other metadata of the queried object without having to manually parse the URL or query the database.

By using get_queried_object, developers can easily customize the display of content based on the specific object being queried, such as changing the layout or adding custom fields based on the type of content being displayed.

The get_queried_object function provides a convenient way to access and manipulate the current queried object within WordPress without having to write complex custom queries or conditionals.

WordPress get_queried_object Function Parameters and Return Value

The get_queried_object function does not accept any parameters.

It returns the queried object, which can be one of the following types: WP_Term, WP_Post_Type, WP_Post, WP_User, or null.

Examples

How to get the queried object in WordPress

Here’s a simple example of using the get_queried_object function to retrieve the queried object in WordPress:

<?php
$queried_object = get_queried_object();

if ( $queried_object ) {
 echo 'Queried object type: ' . $queried_object->post_type;
}
?>

This code snippet retrieves the queried object using the get_queried_object function and then checks if the queried object exists. If it does, it echoes the post type of the queried object.

How to get the queried object ID in WordPress

Here’s an example of using the get_queried_object function to retrieve the ID of the queried object in WordPress:

<?php
$queried_object = get_queried_object();

if ( $queried_object ) {
 echo 'Queried object ID: ' . $queried_object->ID;
}
?>

This code snippet retrieves the queried object using the get_queried_object function and then checks if the queried object exists. If it does, it echoes the ID of the queried object.

How to check if the queried object is a specific post type in WordPress

Here’s an example of using the get_queried_object function to check if the queried object is a specific post type in WordPress:

<?php
$queried_object = get_queried_object();

if ( $queried_object && $queried_object->post_type === 'post' ) {
 echo 'The queried object is a post type.';
} elseif ( $queried_object && $queried_object->post_type === 'page' ) {
 echo 'The queried object is a page type.';
} else {
 echo 'The queried object is not a post or page type.';
}
?>

This code snippet retrieves the queried object using the get_queried_object function and then checks if the queried object exists and if it is a specific post type. It then echoes the result based on the post type of the queried object.

Conclusion

The get_queried_object function is an effective feature for retrieving information about the currently queried object in WordPress. It provides developers with a convenient way to access various properties and metadata of the current post, page, or custom post type. By using this function, developers can streamline their code and improve the efficiency of their WordPress projects.

Whether you need to access the ID, title, or custom fields of the queried object, the get_queried_object function offers a straightforward solution. Its versatility and ease of use make it a valuable asset for WordPress developers looking to enhance their projects with dynamic and customizable content.

Related WordPress Functions