_get_last_post_time

Get the timestamp of the last time any post was modified or published.

Description

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

Returns (string|false)

The timestamp.

Parameters (3)

0. $timezone (string)
The timezone for the timestamp. See get_lastpostdate(). for information on accepted values.
1. $field (string)
Post field to check. Accepts date or modified..
2. $post_type — Optional. (string) => 'any'
The post type to check. Default any..

Usage

  1. if ( !function_exists( '_get_last_post_time' ) ) { 
  2. require_once ABSPATH . WPINC . '/post.php'; 
  3.  
  4. // The timezone for the timestamp. See get_lastpostdate(). 
  5. // for information on accepted values. 
  6. $timezone = ''; 
  7.  
  8. // Post field to check. Accepts 'date' or 'modified'. 
  9. $field = ''; 
  10.  
  11. // Optional. The post type to check. Default 'any'. 
  12. $post_type = 'any'; 
  13.  
  14. // NOTICE! Understand what this does before running. 
  15. $result = _get_last_post_time($timezone, $field, $post_type); 
  16.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/post.php  
  1. function _get_last_post_time( $timezone, $field, $post_type = 'any' ) { 
  2. global $wpdb; 
  3.  
  4. if ( ! in_array( $field, array( 'date', 'modified' ) ) ) { 
  5. return false; 
  6.  
  7. $timezone = strtolower( $timezone ); 
  8.  
  9. $key = "lastpost{$field}:$timezone"; 
  10. if ( 'any' !== $post_type ) { 
  11. $key .= ':' . sanitize_key( $post_type ); 
  12.  
  13. $date = wp_cache_get( $key, 'timeinfo' ); 
  14. if ( false !== $date ) { 
  15. return $date; 
  16.  
  17. if ( 'any' === $post_type ) { 
  18. $post_types = get_post_types( array( 'public' => true ) ); 
  19. array_walk( $post_types, array( $wpdb, 'escape_by_ref' ) ); 
  20. $post_types = "'" . implode( "', '", $post_types ) . "'"; 
  21. } else { 
  22. $post_types = "'" . sanitize_key( $post_type ) . "'"; 
  23.  
  24. switch ( $timezone ) { 
  25. case 'gmt': 
  26. $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"); 
  27. break; 
  28. case 'blog': 
  29. $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"); 
  30. break; 
  31. case 'server': 
  32. $add_seconds_server = date( 'Z' ); 
  33. $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"); 
  34. break; 
  35.  
  36. if ( $date ) { 
  37. wp_cache_set( $key, $date, 'timeinfo' ); 
  38.  
  39. return $date; 
  40.  
  41. return false;