4

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?

6 Answers 6

4

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.

0
8

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 = $wpdb->get_var( $sql );
    echo( "Last updated $last_update." );
?>
2
  • Thanks. The 2nd code block is more what I'm looking for.
    – Eckstein
    Commented Apr 14, 2013 at 17:16
  • Glad you got your answer.
    – Pat J
    Commented Apr 14, 2013 at 19:33
2

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;
}
1
  • Ths is interesting; so how would one turn this into a shortcode to put into the page editor to show the last modified date of a page? I tried turning it into a shortcode with no luck `function last_modified_shortcode() { function wpse95769_modified_date_by_id( $page_id = 0, $format = "F j, Y" ){ global $page; $page = &get_post( $page_id ); setup_postdata( $page ); $modified_time = get_the_modified_time( $format ); wp_reset_postdata( $page ); return $modified_time; } } add_shortcode( 'last_modifed', 'last_modifed_shortcode' );' Commented Dec 4, 2016 at 0:46
1

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

1

It may not be clear from previous answers, but get_post_time() and get_post_modified_time() can be given a post object or post ID. So, to get the published and modified dates by post ID outside the loop:

$published_time = get_post_time( $date_fmt, null, $post_id );
$modified_time = get_post_modified_time( $date_fmt, null, $post_id );

js.

0

What about the get_lastpostmodified( $timezone ) function?

2
  • 3
    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.
    – tfrommen
    Commented Feb 4, 2014 at 16:55
  • How would you tell get_lastpostmodified which post you want?
    – Tom J Nowell
    Commented Feb 4, 2014 at 17:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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