_get_last_post_time( string $timezone, string $field, string $post_type = 'any' ): string|false
- Since
- 3.1.0, 4.4.0
- Source
wp-includes/post.php:7854
Compatibility
- WordPress
- since 4.4.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
$timezonestring- The timezone for the timestamp. See get_lastpostdate().
for information on accepted values. $fieldstring- Post field to check. Accepts 'date' or 'modified'.
$post_typestringoptional- The post type to check. Default 'any'.Default:
'any'
Return value
string|false- The timestamp in 'Y-m-d H:i:s' format, or false on failure.
Performance profile
How much work a call to _get_last_post_time() 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
- 7–79
- Plugin surface
- None
- Called by
- 2
Reaches the database via ->get_var().
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 118.
Nothing here hands control to plugin code.
2 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- cacheobject cache
wp_cache_get()called directly - sqldatabase query
->get_var()called directly - hookthird-party callbacks
apply_filters()one call below _get_last_post_time()
What one call costs · 27 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost _get_last_post_time() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
!$field | 7 | none |
$field && $post_type === "any" && $date !== false | 24 | strtolower(), wp_cache_get() |
$field && $post_type !== "any" && $date !== false | 29 | strtolower(), sanitize_key(), wp_cache_get() |
$field && $date === false | 33–40 | strtolower(), wp_cache_get(), sanitize_key() |
$field && $date === false | 38–45 | strtolower(), wp_cache_get(), sanitize_key(), wp_cache_set() |
$field && $post_type !== "any" && $date === false | 38–45 | strtolower(), sanitize_key(), wp_cache_get(), sanitize_key() |
$field && $post_type === "any" && $date === false | 43–50 | strtolower(), wp_cache_get(), get_post_types() |
$field && $post_type !== "any" && $date === false | 43–50 | strtolower(), sanitize_key(), wp_cache_get(), sanitize_key(), wp_cache_set() |
$field && $date === false | 48–52 | strtolower(), wp_cache_get(), sanitize_key(), ->get_var() |
$field && $post_type === "any" && $date === false | 48–55 | strtolower(), wp_cache_get(), get_post_types(), wp_cache_set() |
$field && $date === false | 48–55 | strtolower(), sanitize_key(), wp_cache_get(), get_post_types() |
$field && $date === false | 53–57 | strtolower(), wp_cache_get(), sanitize_key(), ->get_var(), wp_cache_set() |
15 further outcomes, up to 79 instructions
$field && $date === false | 53–59 | strtolower(), wp_cache_get(), sanitize_key(), gmdate(), ->get_var() |
$field && $post_type !== "any" && $date === false | 53–57 | strtolower(), sanitize_key(), wp_cache_get(), sanitize_key(), ->get_var() |
$field && $date === false | 53–60 | strtolower(), sanitize_key(), wp_cache_get(), get_post_types(), wp_cache_set() |
$field && $post_type === "any" && $date === false | 58–62 | strtolower(), wp_cache_get(), get_post_types(), ->get_var() |
$field && $date === false | 58–64 | strtolower(), wp_cache_get(), sanitize_key(), gmdate(), ->get_var(), wp_cache_set() |
$field && $post_type !== "any" && $date === false | 58–62 | strtolower(), sanitize_key(), wp_cache_get(), sanitize_key(), ->get_var(), wp_cache_set() |
$field && $post_type !== "any" && $date === false | 58–64 | strtolower(), sanitize_key(), wp_cache_get(), sanitize_key(), gmdate(), ->get_var() |
$field && $post_type === "any" && $date === false | 63–67 | strtolower(), wp_cache_get(), get_post_types(), ->get_var(), wp_cache_set() |
$field && $post_type === "any" && $date === false | 63–69 | strtolower(), wp_cache_get(), get_post_types(), gmdate(), ->get_var() |
$field && $date === false | 63–67 | strtolower(), sanitize_key(), wp_cache_get(), get_post_types(), ->get_var() |
$field && $post_type !== "any" && $date === false | 63–69 | strtolower(), sanitize_key(), wp_cache_get(), sanitize_key(), gmdate(), ->get_var(), wp_cache_set() |
$field && $post_type === "any" && $date === false | 68–74 | strtolower(), wp_cache_get(), get_post_types(), gmdate(), ->get_var(), wp_cache_set() |
$field && $date === false | 68–72 | strtolower(), sanitize_key(), wp_cache_get(), get_post_types(), ->get_var(), wp_cache_set() |
$field && $date === false | 68–74 | strtolower(), sanitize_key(), wp_cache_get(), get_post_types(), gmdate(), ->get_var() |
$field && $date === false | 73–79 | strtolower(), sanitize_key(), wp_cache_get(), get_post_types(), gmdate(), ->get_var(), wp_cache_set() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 118 | 7–79 | 9 | |
| 8.5 | 118 | 7–79 | 9 | |
| 8.4 | 118 | 7–79 | 9 | 3 fewer instructions than PHP 8.3 |
| 8.3 | 121 | 7–82 | 9 | |
| 8.2 | 121 | 7–82 | 9 | 1 more instruction than PHP 8.1 |
| 8.1 | 120 | 7–82 | 9 | |
| 7.4 | 120 | 7–82 | 9 |
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 · 4
- sanitize_key()Sanitizes a string key.
- wp_cache_get()Retrieves the cache contents from the cache by key and group.
- get_post_types()Gets a list of all registered post type objects.
- wp_cache_set()Saves the data to the cache.
Used by · 2
- get_lastpostdate()Retrieves the most recent time that a post on the site was published.
- get_lastpostmodified()Gets the most recent time that a post on the site was modified.
Source code
function _get_last_post_time( $timezone, $field, $post_type = 'any' ) { global $wpdb; if ( ! in_array( $field, array( 'date', 'modified' ), true ) ) { return false; } $timezone = strtolower( $timezone ); $key = "lastpost{$field}:$timezone"; if ( 'any' !== $post_type ) { $key .= ':' . sanitize_key( $post_type ); } $date = wp_cache_get( $key, 'timeinfo' ); if ( false !== $date ) { return $date; } if ( 'any' === $post_type ) { $post_types = get_post_types( array( 'public' => true ) ); array_walk( $post_types, array( $wpdb, 'escape_by_ref' ) ); $post_types = "'" . implode( "', '", $post_types ) . "'"; } else { $post_types = "'" . sanitize_key( $post_type ) . "'"; } switch ( $timezone ) { case 'gmt': $date = $wpdb->get_var( "SELECT post_{$field}_gmt FROM $wpdb->posts WHERE post_status = 'publish' AND post_type IN ({$post_types}) ORDER BY post_{$field}_gmt DESC LIMIT 1" ); break; case 'blog': $date = $wpdb->get_var( "SELECT post_{$field} FROM $wpdb->posts WHERE post_status = 'publish' AND post_type IN ({$post_types}) ORDER BY post_{$field}_gmt DESC LIMIT 1" ); break; case 'server': $add_seconds_server = gmdate( 'Z' ); $date = $wpdb->get_var( "SELECT DATE_ADD(post_{$field}_gmt, INTERVAL '$add_seconds_server' SECOND) FROM $wpdb->posts WHERE post_status = 'publish' AND post_type IN ({$post_types}) ORDER BY post_{$field}_gmt DESC LIMIT 1" ); break; } if ( $date ) { wp_cache_set( $key, $date, 'timeinfo' ); return $date; } return false;}Changelog
Introduced in 3.1.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
$post_type argument was added.from the docblockAbout this page
- Parsed data
- Generated from the wordpress-develop 7.1.0 tag, from
src/wp-includes/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.