获取上次修改或发布任何帖子的时间戳。

原型

_get_last_post_time( string $timezone, string $field, string $post_type = 'any' )

参数

$timezone (string) (Required) 时间戳的时区。请参阅get_lastpostdate()。有关可接受值的信息。

$field (string) (Required) 发布字段来检查。接受’日期’或’修改’。

$post_type (string) (Optional)  要检查的帖子类型。

返回值

(string|false)  时间戳。

源文件

路径:wp-includes/post.php

<?php
...
function _get_last_post_time( $timezone, $field, $post_type = 'any' ) {
	global $wpdb;
	if ( ! in_array( $field, array( 'date', 'modified' ) ) ) {
		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 = date( '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;
}
...
?>

其他

英文文档:https://developer.wordpress.org/reference/functions/_get_last_post_time/