WordPress.org

Support

Support » How-To and Troubleshooting » Show Last Updated Date/Time

Show Last Updated Date/Time

  • I would like to show the most recent update Date/Time in my template.

    Being completely green when it comes to php, can some kind soul show me the simplest way of displaying the result of:
    ‘select max(post_date) from wp_posts’

    Thanks.

Viewing 12 replies - 1 through 12 (of 12 total)
  • Same here : i would like to display the date of the last update of the whole site (in my footer). Any idea ?

    same

    this might help you:

    /* site_last_updated
    *
    * This will check the modified_date of all posts and echo the most recent date.
    *
    */
    function site_last_updated($d = '') {
      $last_update = 0;
      if ( have_posts() ) :
        while ( have_posts() ) :
          the_post();
          // check for current post's modified_date
          if (get_the_modified_date('U') > $last_update) {
            $last_update = get_the_modified_date('U');
          }
        endwhile;
        // format output
        if ( '' == $d )
          $last_update = date(get_option('date_format'), $last_update);
        else
          $last_update = date($d, $last_update);
        // output
        echo $last_update;
      else:
        echo 'No posts.';
      endif;
    }

    What is the point of this? The last post you make automatically goes to the top – or first post – of your site. It gives the date and time of the post. That is exactly what you want to do right there. If the format for the date and time is not exactly as you want it you need only to change the includes on your page.php file.

    That’s why the function isn’t called when_was_the_last_post_added! 😉
    If you edit an existing post, it doesn’t change the order of appearance on your site, but it’s well an update (in my eyes). That’s why it checks the modified_date of all posts, not only of the last post.

    My function above didn’t do exactly what it was supposed to do and was too complicated anyway. Here’s the update:

    /* site_last_updated
    *
    * This will check the modified_date of all posts and give you the most recent date.
    *
    */
    function site_last_updated($d = '') {
    	$allposts = new WP_Query();
    	$allposts->query('orderby=modified&order=ASC');
    	if ( $allposts->have_posts() ) {
    		$allposts->the_post();
    		$last_update = the_modified_date($d);
    		echo $last_update;
    	}
    	else
    		echo 'No posts.';
    }

    Hi,

    I’d was looking for something like this but I am new to WordPress and I am not that sure about how to use it.

    I pasted the function into the functions.php file.
    Is that the thing to do?

    How do I call this function in my text to make the last updated date appear there?

    TIA

    Hi Digi,

    I pasted the function into the functions.php file.
    Is that the thing to do?

    Yes! Although you’ll have to update it with the version below.

    How do I call this function in my text to make the last updated date appear there?

    You have to put this in your template at the desired place:
    <?php site_last_updated('') ?>

    And finally a version of my funtion, that actually does it’s job properly:

    /* site_last_updated
    *
    * This will check the modified_date of all published posts and give you the most recent date.
    *
    */
    function site_last_updated($d = '') {
    	$recent = new WP_Query("showposts=1&orderby=modified&post_status=publish");
    	if ( $recent->have_posts() ) {
    		while ( $recent->have_posts() ) {
    			$recent->the_post();
    			$last_update = get_the_modified_date($d);
    		}
    		echo $last_update;
    	}
    	else
    		echo 'No posts.';
    }

    The function accepts the standard parameters for date/time-formatting.

    Hi,

    thanks for helping me.

    I am trying to use it in a txt widget, unfortunately nothing shows 🙁

    Now that I have pasted it into the php file of one of the sidebars I am using, it does show.

    Is it impossible to use it in a txt widget? Or should I use another command?

    TIA

    Have a look at this plugin, it enables you to include raw php-functions in a widget.

    Thank you very very much, it works now.

    I am still playing around with the date format, but I am happy it is working so far.

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘Show Last Updated Date/Time’ is closed to new replies.
Skip to toolbar