How to display lastest post date in the homepage?

I would save an option on post save:

add_action(
  'save_post',
  function($id,$p) {
    if (
      (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) 
      || (defined('DOING_AJAX') && DOING_AJAX)
      || ($p->post_status === 'auto-draft')
    ) {
      return;
    }
    update_option('_last_site_update',$p->post_date);
  },
  1,2
);

And retrieve it with a variant of the function provided by @G-M :

function my_last_updated( $format="" ) {
  $last = get_option('_last_site_update');
  if ( empty($last) ) return;
  if ( empty($format) ) {
    $format = get_option('date_format');
  }
  return mysql2date($format,$last);
}
echo my_last_updated();

This way you:

  1. push the heavy lifting to the admin side,
  2. eliminate the unnecessary work of a full post query (via
    wp_get_recent_posts
    ),
  3. replace that heavy query with a very simple get_option query,
  4. and make the whole thing cache-friendly

Leave a Comment