Get date of last update outside of loop

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?

Related posts

Leave a Reply

6 comments

  1. 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.

  2. 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." );
    ?>
    
  3. 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;
    }
    
  4. 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

  5. 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.