WP_Post::__get( string $key ): mixed
- Since
- 3.5.0
- Source
wp-includes/class-wp-post.php:307
Compatibility
- WordPress
- since 3.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.
Parameters
$keystring- Key to get.
Return value
mixed
Performance profile
How much work a call to WP_Post::__get() 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
- 15–35
- Plugin surface
- None
- Called by
- 1
Reaches the database via get_post().
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 91.
Nothing here hands control to plugin code.
1 place in core call this, so the cost is paid more often than your own code shows.
What it touches
- querycontent query
get_post()one call below WP_Post::__get() - cacheobject cache
wp_cache_add()one call below WP_Post::__get() - hookthird-party callbacks
apply_filters()one call below WP_Post::__get()
Further down the call graph this can also reach serialize, option and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.
What one call costs · 16 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_Post::__get() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
$key !== "page_template" && !is_object_in_taxonomy() && empty($terms) | 15–17 | is_object_in_taxonomy() |
$key === "page_template" | 15–24 | ->__isset(), get_post_meta() |
$key !== "page_template" && $key !== "post_category" && $key !== "tags_input" && $key === "ancestors" | 18 | get_post_ancestors() |
$key !== "page_template" && !is_object_in_taxonomy() && !empty($terms) | 19–21 | is_object_in_taxonomy(), wp_list_pluck() |
$key === "page_template" && !->__isset() && !is_object_in_taxonomy() && empty($terms) | 19–21 | ->__isset(), is_object_in_taxonomy() |
$key !== "page_template" && $key !== "post_category" && $key !== "tags_input" && $key !== "ancestors" | 20 | get_post_meta() |
$key !== "page_template" && is_object_in_taxonomy() && empty($terms) | 21–23 | is_object_in_taxonomy(), get_the_terms() |
$key === "page_template" && !->__isset() && $key !== "post_category" && $key !== "tags_input" && $key === "ancestors" | 22 | ->__isset(), get_post_ancestors() |
$key === "page_template" && !->__isset() && !is_object_in_taxonomy() && !empty($terms) | 23–25 | ->__isset(), is_object_in_taxonomy(), wp_list_pluck() |
$key !== "page_template" && is_object_in_taxonomy() && !empty($terms) | 25–27 | is_object_in_taxonomy(), get_the_terms(), wp_list_pluck() |
$key === "page_template" && !->__isset() && is_object_in_taxonomy() && empty($terms) | 25–27 | ->__isset(), is_object_in_taxonomy(), get_the_terms() |
$key !== "page_template" && $key !== "post_category" && $key !== "tags_input" && $key === "ancestors" | 29 | get_post_ancestors(), sanitize_post_field() |
4 further outcomes, up to 35 instructions
$key === "page_template" && !->__isset() && is_object_in_taxonomy() && !empty($terms) | 29–31 | ->__isset(), is_object_in_taxonomy(), get_the_terms(), wp_list_pluck() |
$key !== "page_template" && $key !== "post_category" && $key !== "tags_input" && $key !== "ancestors" | 31 | get_post_meta(), sanitize_post_field() |
$key === "page_template" && !->__isset() && $key !== "post_category" && $key !== "tags_input" && $key === "ancestors" | 33 | ->__isset(), get_post_ancestors(), sanitize_post_field() |
$key === "page_template" && !->__isset() && $key !== "post_category" && $key !== "tags_input" && $key !== "ancestors" | 35 | ->__isset(), get_post_meta(), sanitize_post_field() |
Across PHP versions
Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 91 instructions, 15–35 executed per call, 10 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 · 7
- get_post_meta()Retrieves a post meta field for the given post ID.
- is_object_in_taxonomy()Determines if the given object type is associated with the given taxonomy.
- get_the_terms()Retrieves the terms of the taxonomy that are attached to the post.
- wp_list_pluck()Plucks a certain field out of each object or array in an array.
- get_post_ancestors()Retrieves the IDs of the ancestors of a post.
- sanitize_post_field()Sanitizes a post field based on context.
- WP_Post::__isset()Isset-er.
Used by · 1
- WP_Post::to_array()Convert object to array.
Source code
public function __get( $key ) { if ( 'page_template' === $key && $this->__isset( $key ) ) { return get_post_meta( $this->ID, '_wp_page_template', true ); } if ( 'post_category' === $key ) { if ( is_object_in_taxonomy( $this->post_type, 'category' ) ) { $terms = get_the_terms( $this, 'category' ); } if ( empty( $terms ) ) { return array(); } return wp_list_pluck( $terms, 'term_id' ); } if ( 'tags_input' === $key ) { if ( is_object_in_taxonomy( $this->post_type, 'post_tag' ) ) { $terms = get_the_terms( $this, 'post_tag' ); } if ( empty( $terms ) ) { return array(); } return wp_list_pluck( $terms, 'name' ); } // Rest of the values need filtering. if ( 'ancestors' === $key ) { $value = get_post_ancestors( $this ); } else { $value = get_post_meta( $this->ID, $key, true ); } if ( $this->filter ) { $value = sanitize_post_field( $key, $value, $this->ID, $this->filter ); } return $value; }Changelog
Introduced in 3.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.9.7 tag, from
src/wp-includes/class-wp-post.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.