WordPress.org

Codex

Interested in functions, hooks, classes, or methods? Check out the new WordPress Code Reference!

Function Reference/get queried object

Description

Retrieve the currently-queried object. For example:

  • if you're on a single post, it will return the post object
  • if you're on a page, it will return the page object
  • if you're on an archive page, it will return the post type object
  • if you're on a category archive, it will return the category object
  • if you're on an author archive, it will return the author object
  • etc.

Note that precedence plays an important role. As an example, if you visit a custom post type archive while also passing in a taxonomy term (ie. /my-post-type/?my_taxonomy=term), the request is both a post archive and a taxonomy archive. In this case it isn't obvious that get_queried_object() will return the term object and not the post type object (as of WordPress 4.3). The implication is that you cannot necessarily rely on get_queried_object() returning a post type simply because is_post_type_archive() is true.

Wrapper for $wp_query->get_queried_object().

Usage

 <?php
 $queried_object = get_queried_object();
 var_dump( $queried_object );
 ?>

Parameters

None.

Return Values

(object) 

Examples

Notes

  • Uses: WP_Query::get_queried_object
  • Uses global: (object) $wp_query

Change Log

Since: 3.1.0

Source File

get_queried_object() is located in /wp-includes/query.php.

Related