Skip to main content
Fixed a Syntax Error with WPDB and added some link references.
Source Link
Howdy_McGee
  • 20.8k
  • 24
  • 91
  • 175

According to the Codex page for get_the_time(), it needs to be used in The Loop. The difference between the_time()the_time() and get_the_time()get_the_time() is that the former echo()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 LoopThe 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$wpdb->get_var( $sql );
    echo( "Last updated $last_update." );
?>

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." );
?>

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." );
?>
added $wpdb suggestion
Source Link
Pat J
  • 12.3k
  • 2
  • 28
  • 36

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." );
?>

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." );
?>

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." );
?>
Source Link
Pat J
  • 12.3k
  • 2
  • 28
  • 36

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." );
?>