Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm trying to figure out how to display the date that a post was last updated outside of the loop. I'm able to display the published date using get_the_time() but there doesn't seem to be a "loopless" function to get the date of the last update.

Does anyone know hot to do this?

share|improve this question
add comment

5 Answers

up vote 3 down vote accepted

It is unclear if you are looking for the last updated post or for the last updated date for some particular post. The answer by @PatJ assumes the former. To do the latter:

$qry = new WP_Query(array('p'=>1));
var_dump($qry->posts[0]->post_modified);

Or...

$date = $wpdb->get_var("SELECT post_modified FROM {$wpdb->posts} WHERE ID = 1");
var_dump($date);

Of course you need to change the post ID to match the post you are looking for.

share|improve this answer
    
Thank you, this is what I'm looking for! –  Eckstein Apr 14 '13 at 17:15
add comment

According to the Codex page for get_the_time(), it needs to be used in The Loop. The difference between the_time() and get_the_time() is that the former echo()es the date, and the latter returns it.

There are a couple functions that do what I think you're looking for -- get the last updated date and time for a post: get_the_modified_time() and get_the_modified_date(). It looks like they, too, need to be used in The Loop.

Here's one way to get the updated date of the most recent post in your site:

<?php
     $args = array(
        'orderby' => 'post_modified',
        'numberposts' => 1,
    );
    $myposts = get_posts( $args );
    if( have_posts() ) {
        while( have_posts() ) {
            the_post();
            $last_update = get_the_modified_date();
        }
    }
    echo( "Last modified on $last_update." );
?>

If you're sure you need to be outside of any Loops, you can always use $wpdb:

<?php
    global $wpdb;
    $sql = "SELECT post_modified
            FROM $wpdb->posts
            WHERE post_type='post'
            AND post_status='publish'
            ORDER BY post_modified DESC
            LIMIT 1";
    $last_update = $wdpb->get_var( $sql );
    echo( "Last updated $last_update." );
?>
share|improve this answer
    
Thanks. The 2nd code block is more what I'm looking for. –  Eckstein Apr 14 '13 at 17:16
    
Glad you got your answer. –  Pat J Apr 14 '13 at 19:33
add comment

Another late addition, which may be helpful to know if anyone is looking. You can use these two functions to get the post date and modified date outside the loop.

<?php get_post_time( $d, $gmt, $post, $translate ); ?> 

and

<?php get_post_modified_time( $d, $gmt, $post, $translate ); ?>

You can read more about these two functions in the codex.

http://codex.wordpress.org/Template_Tags/get_post_time http://codex.wordpress.org/Function_Reference/get_post_modified_time

share|improve this answer
add comment

A late addition, but the following snippet can be modified to use most functions outside of the loop:

/**
 * Returns a post's modified date, formatted according to $format.
 * @uses the_modified_time()
 *
 * @param int $post_id Post ID.
 * @param string $format Date format Default: "F j, Y".
 */
function wpse95769_modified_date_by_id( $post_id = 0, $format = "F j, Y" ){
    global $post;
    $post = &get_post( $post_id );
    setup_postdata( $post );

    $modified_time = get_the_modified_time( $format );

    wp_reset_postdata( $post );

    return $modified_time;
}
share|improve this answer
add comment

What about the get_lastpostmodified( $timezone ) function?

share|improve this answer
2  
This returns the date when the last post has been modified. So this function is not post-specific but blog-specific - and hence not what was asked for. –  t f Feb 4 at 16:55
    
How would you tell get_lastpostmodified which post you want? –  Tom J Nowell Feb 4 at 17:15
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.