wppaste
WordPress

WP_Query::get_queried_object(): WP_Term|WP_Post_Type|WP_Post|WP_User|null

Since
1.5.0
Source
wp-includes/class-wp-query.php:3976
Retrieves the currently queried object.

Description

If queried object is not set, then the queried object will be set from the category, tag, taxonomy, posts page, single post, page, or author query variable. After it is set up, it will be returned.

Compatibility

WordPress
since 1.5.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Return value

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

Performance profile

How much work a call to WP_Query::get_queried_object() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Heavy

Reaches the database via get_term().

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
4–75

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 212.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
9

9 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • querycontent queryget_term()called directly
  • optionoption read or writeget_option()called directly
  • hookthird-party callbacksapply_filters()one call below WP_Query::get_queried_object()
  • cacheobject cachewp_cache_get()one call below WP_Query::get_queried_object()
  • serializeserialisationmaybe_unserialize()one call below WP_Query::get_queried_object()

Further down the call graph this can also reach transient. That is the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 26 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_Query::get_queried_object() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
always4–31none
!isset($value) && empty($value) && !empty($term)23–39is_wp_error()
!isset($value) && empty($term)24–30->get(), ->get()
!isset($value) && !is_array($post_type)28->get(), get_post_type_object()
!isset($value) && !empty($term)28–46->get(), ->get(), is_wp_error()
!isset($value) && empty($term)29–35->get(), ->get(), get_term()
!isset($value) && empty($term)31–37->get(), ->get(), get_term_by()
!isset($value) && is_array($post_type)32->get(), reset(), get_post_type_object()
!isset($value)33get_option(), get_post()
!isset($value) && !empty($term)33–51->get(), ->get(), get_term(), is_wp_error()
!isset($value) && !empty($value) && empty($query) && empty($term)34–38array_keys(), reset()
!isset($value) && !empty($term)35–53->get(), ->get(), get_term_by(), is_wp_error()
14 further outcomes, up to 75 instructions
!isset($value) && !empty($value) && empty($query) && !empty($term)38–54array_keys(), reset(), is_wp_error()
!isset($value)39–43->get(), ->get(), get_userdata()
!isset($value) && empty($value) && !empty($term) && !is_wp_error()41–45is_wp_error(), _make_cat_compat()
!isset($value)45–50->get(), ->get(), get_user_by(), get_userdata()
!isset($value) && !empty($term) && !is_wp_error()46–52->get(), ->get(), is_wp_error(), _make_cat_compat()
!isset($value) && !empty($value) && !empty($query) && empty($term)47–51array_keys(), reset(), reset(), get_term()
!isset($value) && !empty($value) && !empty($query) && empty($term)49–53array_keys(), reset(), reset(), get_term_by()
!isset($value) && !empty($value) && !empty($query) && !empty($term)51–67array_keys(), reset(), reset(), get_term(), is_wp_error()
!isset($value) && !empty($term) && !is_wp_error()51–57->get(), ->get(), get_term(), is_wp_error(), _make_cat_compat()
!isset($value) && !empty($value) && !empty($query) && !empty($term)53–69array_keys(), reset(), reset(), get_term_by(), is_wp_error()
!isset($value) && !empty($term) && !is_wp_error()53–59->get(), ->get(), get_term_by(), is_wp_error(), _make_cat_compat()
!isset($value) && !empty($value) && empty($query) && !empty($term) && !is_wp_error()56–60array_keys(), reset(), is_wp_error(), _make_cat_compat()
!isset($value) && !empty($value) && !empty($query) && !empty($term) && !is_wp_error()69–73array_keys(), reset(), reset(), get_term(), is_wp_error(), _make_cat_compat()
!isset($value) && !empty($value) && !empty($query) && !empty($term) && !is_wp_error()71–75array_keys(), reset(), reset(), get_term_by(), is_wp_error(), _make_cat_compat()

Across PHP versions

Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 212 instructions, 4–75 executed per call, 26 branches. The work does not change between versions.

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Uses · 10

Used by · 9

Source code

	public function get_queried_object() {		if ( isset( $this->queried_object ) ) {			return $this->queried_object;		} 		$this->queried_object    = null;		$this->queried_object_id = null; 		if ( $this->is_category || $this->is_tag || $this->is_tax ) {			if ( $this->is_category ) {				$cat           = $this->get( 'cat' );				$category_name = $this->get( 'category_name' ); 				if ( $cat ) {					$term = get_term( $cat, 'category' );				} elseif ( $category_name ) {					$term = get_term_by( 'slug', $category_name, 'category' );				}			} elseif ( $this->is_tag ) {				$tag_id = $this->get( 'tag_id' );				$tag    = $this->get( 'tag' ); 				if ( $tag_id ) {					$term = get_term( $tag_id, 'post_tag' );				} elseif ( $tag ) {					$term = get_term_by( 'slug', $tag, 'post_tag' );				}			} else {				// For other tax queries, grab the first term from the first clause.				if ( ! empty( $this->tax_query->queried_terms ) ) {					$queried_taxonomies = array_keys( $this->tax_query->queried_terms );					$matched_taxonomy   = reset( $queried_taxonomies );					$query              = $this->tax_query->queried_terms[ $matched_taxonomy ]; 					if ( ! empty( $query['terms'] ) ) {						if ( 'term_id' === $query['field'] ) {							$term = get_term( reset( $query['terms'] ), $matched_taxonomy );						} else {							$term = get_term_by( $query['field'], reset( $query['terms'] ), $matched_taxonomy );						}					}				}			} 			if ( ! empty( $term ) && ! is_wp_error( $term ) ) {				$this->queried_object    = $term;				$this->queried_object_id = (int) $term->term_id; 				if ( $this->is_category && 'category' === $this->queried_object->taxonomy ) {					_make_cat_compat( $this->queried_object );				}			}		} elseif ( $this->is_post_type_archive ) {			$post_type = $this->get( 'post_type' ); 			if ( is_array( $post_type ) ) {				$post_type = reset( $post_type );			} 			$this->queried_object = get_post_type_object( $post_type );		} elseif ( $this->is_posts_page ) {			$page_for_posts = get_option( 'page_for_posts' ); 			$this->queried_object    = get_post( $page_for_posts );			$this->queried_object_id = (int) $this->queried_object->ID;		} elseif ( $this->is_singular && ! empty( $this->post ) ) {			$this->queried_object    = $this->post;			$this->queried_object_id = (int) $this->post->ID;		} elseif ( $this->is_author ) {			$author      = (int) $this->get( 'author' );			$author_name = $this->get( 'author_name' ); 			if ( $author ) {				$this->queried_object_id = $author;			} elseif ( $author_name ) {				$user = get_user_by( 'slug', $author_name ); 				if ( $user ) {					$this->queried_object_id = $user->ID;				}

Changelog

Introduced in 1.5.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 6.9.7 tag, from src/wp-includes/class-wp-query.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.