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:3967
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
- Scaling
- Constant
- Instructions
- 4–75
- Plugin surface
- None
- Called by
- 9
Reaches the database via get_term().
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 212.
Nothing here hands control to plugin code.
9 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- querycontent query
get_term()called directly - optionoption read or write
get_option()called directly - hookthird-party callbacks
apply_filters()one call below WP_Query::get_queried_object() - cacheobject cache
wp_cache_get()one call below WP_Query::get_queried_object() - serializeserialisation
maybe_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.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 4–31 | none |
!isset($value) && empty($value) && !empty($term) | 23–39 | is_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) | 33 | get_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–38 | array_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–54 | array_keys(), reset(), is_wp_error() |
!isset($value) | 39–43 | ->get(), ->get(), get_userdata() |
!isset($value) && empty($value) && !empty($term) && !is_wp_error() | 41–45 | is_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–51 | array_keys(), reset(), reset(), get_term() |
!isset($value) && !empty($value) && !empty($query) && empty($term) | 49–53 | array_keys(), reset(), reset(), get_term_by() |
!isset($value) && !empty($value) && !empty($query) && !empty($term) | 51–67 | array_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–69 | array_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–60 | array_keys(), reset(), is_wp_error(), _make_cat_compat() |
!isset($value) && !empty($value) && !empty($query) && !empty($term) && !is_wp_error() | 69–73 | array_keys(), reset(), reset(), get_term(), is_wp_error(), _make_cat_compat() |
!isset($value) && !empty($value) && !empty($query) && !empty($term) && !is_wp_error() | 71–75 | array_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
- get_term()Gets all term data from database by term ID.
- get_term_by()Gets all term data from database by term field and data.
- is_wp_error()Checks whether the given variable is a WordPress Error.
- _make_cat_compat()Updates category structure to old pre-2.3 from new taxonomy structure.
- get_post_type_object()Retrieves a post type object by name.
- get_option()Retrieves an option value based on an option name.
- get_post()Retrieves post data given a post ID or post object.
- get_user_by()Retrieves user info by a given field.
- get_userdata()Retrieves user info by user ID.
- WP_Query::get()Retrieves the value of a query variable.
Used by · 9
- WP_Query::get_queried_object_id()Retrieves the ID of the currently queried object.
- WP_Query::is_attachment()Determines whether the query is for an existing attachment page.
- WP_Query::is_author()Determines whether the query is for an existing author archive page.
- WP_Query::is_category()Determines whether the query is for an existing category archive page.
- WP_Query::is_page()Determines whether the query is for an existing single page.
- WP_Query::is_single()Determines whether the query is for an existing single post.
- WP_Query::is_singular()Determines whether the query is for an existing single post of any post type (post, attachment, page, custom post types).
- WP_Query::is_tag()Determines whether the query is for an existing tag archive page.
- WP_Query::is_tax()Determines whether the query is for an existing custom taxonomy archive page.
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.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 6.8.8 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.