_get_last_post_time

The timeline below displays how wordpress function _get_last_post_time has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 5.5

/**
 * Gets the timestamp of the last time any post was modified or published.
 *
 * @since 3.1.0
 * @since 4.4.0 The `$post_type` argument was added.
 * @access private
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @param string $timezone  The timezone for the timestamp. See get_lastpostdate().
 *                          for information on accepted values.
 * @param string $field     Post field to check. Accepts 'date' or 'modified'.
 * @param string $post_type Optional. The post type to check. Default 'any'.
 * @return string|false The timestamp in 'Y-m-d H:i:s' format, or false on failure.
 */
function _get_last_post_time($timezone, $field, $post_type = 'any')
{
    global $wpdb;
    if (!in_array($field, array('date', 'modified'), true)) {
        return false;
    }
    $timezone = strtolower($timezone);
    $key = "lastpost{$field}:{$timezone}";
    if ('any' !== $post_type) {
        $key .= ':' . sanitize_key($post_type);
    }
    $date = wp_cache_get($key, 'timeinfo');
    if (false !== $date) {
        return $date;
    }
    if ('any' === $post_type) {
        $post_types = get_post_types(array('public' => true));
        array_walk($post_types, array($wpdb, 'escape_by_ref'));
        $post_types = "'" . implode("', '", $post_types) . "'";
    } else {
        $post_types = "'" . sanitize_key($post_type) . "'";
    }
    switch ($timezone) {
        case 'gmt':
            $date = $wpdb->get_var("SELECT post_{$field}_gmt FROM {$wpdb->posts} WHERE post_status = 'publish' AND post_type IN ({$post_types}) ORDER BY post_{$field}_gmt DESC LIMIT 1");
            break;
        case 'blog':
            $date = $wpdb->get_var("SELECT post_{$field} FROM {$wpdb->posts} WHERE post_status = 'publish' AND post_type IN ({$post_types}) ORDER BY post_{$field}_gmt DESC LIMIT 1");
            break;
        case 'server':
            $add_seconds_server = gmdate('Z');
            $date = $wpdb->get_var("SELECT DATE_ADD(post_{$field}_gmt, INTERVAL '{$add_seconds_server}' SECOND) FROM {$wpdb->posts} WHERE post_status = 'publish' AND post_type IN ({$post_types}) ORDER BY post_{$field}_gmt DESC LIMIT 1");
            break;
    }
    if ($date) {
        wp_cache_set($key, $date, 'timeinfo');
        return $date;
    }
    return false;
}

WordPress Version: .10

/**
 * Gets the timestamp of the last time any post was modified or published.
 *
 * @since 3.1.0
 * @since 4.4.0 The `$post_type` argument was added.
 * @access private
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @param string $timezone  The timezone for the timestamp. See get_lastpostdate().
 *                          for information on accepted values.
 * @param string $field     Post field to check. Accepts 'date' or 'modified'.
 * @param string $post_type Optional. The post type to check. Default 'any'.
 * @return string|false The timestamp in 'Y-m-d H:i:s' format, or false on failure.
 */
function _get_last_post_time($timezone, $field, $post_type = 'any')
{
    global $wpdb;
    if (!in_array($field, array('date', 'modified'))) {
        return false;
    }
    $timezone = strtolower($timezone);
    $key = "lastpost{$field}:{$timezone}";
    if ('any' !== $post_type) {
        $key .= ':' . sanitize_key($post_type);
    }
    $date = wp_cache_get($key, 'timeinfo');
    if (false !== $date) {
        return $date;
    }
    if ('any' === $post_type) {
        $post_types = get_post_types(array('public' => true));
        array_walk($post_types, array($wpdb, 'escape_by_ref'));
        $post_types = "'" . implode("', '", $post_types) . "'";
    } else {
        $post_types = "'" . sanitize_key($post_type) . "'";
    }
    switch ($timezone) {
        case 'gmt':
            $date = $wpdb->get_var("SELECT post_{$field}_gmt FROM {$wpdb->posts} WHERE post_status = 'publish' AND post_type IN ({$post_types}) ORDER BY post_{$field}_gmt DESC LIMIT 1");
            break;
        case 'blog':
            $date = $wpdb->get_var("SELECT post_{$field} FROM {$wpdb->posts} WHERE post_status = 'publish' AND post_type IN ({$post_types}) ORDER BY post_{$field}_gmt DESC LIMIT 1");
            break;
        case 'server':
            $add_seconds_server = gmdate('Z');
            $date = $wpdb->get_var("SELECT DATE_ADD(post_{$field}_gmt, INTERVAL '{$add_seconds_server}' SECOND) FROM {$wpdb->posts} WHERE post_status = 'publish' AND post_type IN ({$post_types}) ORDER BY post_{$field}_gmt DESC LIMIT 1");
            break;
    }
    if ($date) {
        wp_cache_set($key, $date, 'timeinfo');
        return $date;
    }
    return false;
}

WordPress Version: 5.3

/**
 * Gets the timestamp of the last time any post was modified or published.
 *
 * @since 3.1.0
 * @since 4.4.0 The `$post_type` argument was added.
 * @access private
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @param string $timezone  The timezone for the timestamp. See get_lastpostdate().
 *                          for information on accepted values.
 * @param string $field     Post field to check. Accepts 'date' or 'modified'.
 * @param string $post_type Optional. The post type to check. Default 'any'.
 * @return string|false The timestamp in 'Y-m-d H:i:s' format, or false on error.
 */
function _get_last_post_time($timezone, $field, $post_type = 'any')
{
    global $wpdb;
    if (!in_array($field, array('date', 'modified'))) {
        return false;
    }
    $timezone = strtolower($timezone);
    $key = "lastpost{$field}:{$timezone}";
    if ('any' !== $post_type) {
        $key .= ':' . sanitize_key($post_type);
    }
    $date = wp_cache_get($key, 'timeinfo');
    if (false !== $date) {
        return $date;
    }
    if ('any' === $post_type) {
        $post_types = get_post_types(array('public' => true));
        array_walk($post_types, array($wpdb, 'escape_by_ref'));
        $post_types = "'" . implode("', '", $post_types) . "'";
    } else {
        $post_types = "'" . sanitize_key($post_type) . "'";
    }
    switch ($timezone) {
        case 'gmt':
            $date = $wpdb->get_var("SELECT post_{$field}_gmt FROM {$wpdb->posts} WHERE post_status = 'publish' AND post_type IN ({$post_types}) ORDER BY post_{$field}_gmt DESC LIMIT 1");
            break;
        case 'blog':
            $date = $wpdb->get_var("SELECT post_{$field} FROM {$wpdb->posts} WHERE post_status = 'publish' AND post_type IN ({$post_types}) ORDER BY post_{$field}_gmt DESC LIMIT 1");
            break;
        case 'server':
            $add_seconds_server = gmdate('Z');
            $date = $wpdb->get_var("SELECT DATE_ADD(post_{$field}_gmt, INTERVAL '{$add_seconds_server}' SECOND) FROM {$wpdb->posts} WHERE post_status = 'publish' AND post_type IN ({$post_types}) ORDER BY post_{$field}_gmt DESC LIMIT 1");
            break;
    }
    if ($date) {
        wp_cache_set($key, $date, 'timeinfo');
        return $date;
    }
    return false;
}

WordPress Version: 4.7

/**
 * Get the timestamp of the last time any post was modified or published.
 *
 * @since 3.1.0
 * @since 4.4.0 The `$post_type` argument was added.
 * @access private
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @param string $timezone  The timezone for the timestamp. See get_lastpostdate().
 *                          for information on accepted values.
 * @param string $field     Post field to check. Accepts 'date' or 'modified'.
 * @param string $post_type Optional. The post type to check. Default 'any'.
 * @return string|false The timestamp.
 */
function _get_last_post_time($timezone, $field, $post_type = 'any')
{
    global $wpdb;
    if (!in_array($field, array('date', 'modified'))) {
        return false;
    }
    $timezone = strtolower($timezone);
    $key = "lastpost{$field}:{$timezone}";
    if ('any' !== $post_type) {
        $key .= ':' . sanitize_key($post_type);
    }
    $date = wp_cache_get($key, 'timeinfo');
    if (false !== $date) {
        return $date;
    }
    if ('any' === $post_type) {
        $post_types = get_post_types(array('public' => true));
        array_walk($post_types, array($wpdb, 'escape_by_ref'));
        $post_types = "'" . implode("', '", $post_types) . "'";
    } else {
        $post_types = "'" . sanitize_key($post_type) . "'";
    }
    switch ($timezone) {
        case 'gmt':
            $date = $wpdb->get_var("SELECT post_{$field}_gmt FROM {$wpdb->posts} WHERE post_status = 'publish' AND post_type IN ({$post_types}) ORDER BY post_{$field}_gmt DESC LIMIT 1");
            break;
        case 'blog':
            $date = $wpdb->get_var("SELECT post_{$field} FROM {$wpdb->posts} WHERE post_status = 'publish' AND post_type IN ({$post_types}) ORDER BY post_{$field}_gmt DESC LIMIT 1");
            break;
        case 'server':
            $add_seconds_server = date('Z');
            $date = $wpdb->get_var("SELECT DATE_ADD(post_{$field}_gmt, INTERVAL '{$add_seconds_server}' SECOND) FROM {$wpdb->posts} WHERE post_status = 'publish' AND post_type IN ({$post_types}) ORDER BY post_{$field}_gmt DESC LIMIT 1");
            break;
    }
    if ($date) {
        wp_cache_set($key, $date, 'timeinfo');
        return $date;
    }
    return false;
}

WordPress Version: 4.4

/**
 * Get the timestamp of the last time any post was modified or published.
 *
 * @since 3.1.0
 * @since 4.4.0 The `$post_type` argument was added.
 * @access private
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @param string $timezone  The timezone for the timestamp. See get_lastpostdate().
 *                          for information on accepted values.
 * @param string $field     Post field to check. Accepts 'date' or 'modified'.
 * @param string $post_type Optional. The post type to check. Default 'any'.
 * @return string|false The timestamp.
 */
function _get_last_post_time($timezone, $field, $post_type = 'any')
{
    global $wpdb;
    if (!in_array($field, array('date', 'modified'))) {
        return false;
    }
    $timezone = strtolower($timezone);
    $key = "lastpost{$field}:{$timezone}";
    if ('any' !== $post_type) {
        $key .= ':' . sanitize_key($post_type);
    }
    $date = wp_cache_get($key, 'timeinfo');
    if (!$date) {
        if ('any' === $post_type) {
            $post_types = get_post_types(array('public' => true));
            array_walk($post_types, array($wpdb, 'escape_by_ref'));
            $post_types = "'" . implode("', '", $post_types) . "'";
        } else {
            $post_types = "'" . sanitize_key($post_type) . "'";
        }
        switch ($timezone) {
            case 'gmt':
                $date = $wpdb->get_var("SELECT post_{$field}_gmt FROM {$wpdb->posts} WHERE post_status = 'publish' AND post_type IN ({$post_types}) ORDER BY post_{$field}_gmt DESC LIMIT 1");
                break;
            case 'blog':
                $date = $wpdb->get_var("SELECT post_{$field} FROM {$wpdb->posts} WHERE post_status = 'publish' AND post_type IN ({$post_types}) ORDER BY post_{$field}_gmt DESC LIMIT 1");
                break;
            case 'server':
                $add_seconds_server = date('Z');
                $date = $wpdb->get_var("SELECT DATE_ADD(post_{$field}_gmt, INTERVAL '{$add_seconds_server}' SECOND) FROM {$wpdb->posts} WHERE post_status = 'publish' AND post_type IN ({$post_types}) ORDER BY post_{$field}_gmt DESC LIMIT 1");
                break;
        }
        if ($date) {
            wp_cache_set($key, $date, 'timeinfo');
        }
    }
    return $date;
}

WordPress Version: 4.3

/**
 * Get the timestamp of the last time any post was modified or published.
 *
 * @since 3.1.0
 * @access private
 *
 * @global wpdb $wpdb
 *
 * @param string $timezone The timezone for the timestamp. See {@see get_lastpostmodified()}
 *                         for information on accepted values.
 * @param string $field    Post field to check. Accepts 'date' or 'modified'.
 * @return string|false The timestamp.
 */
function _get_last_post_time($timezone, $field)
{
    global $wpdb;
    if (!in_array($field, array('date', 'modified'))) {
        return false;
    }
    $timezone = strtolower($timezone);
    $key = "lastpost{$field}:{$timezone}";
    $date = wp_cache_get($key, 'timeinfo');
    if (!$date) {
        $add_seconds_server = date('Z');
        $post_types = get_post_types(array('public' => true));
        array_walk($post_types, array(&$wpdb, 'escape_by_ref'));
        $post_types = "'" . implode("', '", $post_types) . "'";
        switch ($timezone) {
            case 'gmt':
                $date = $wpdb->get_var("SELECT post_{$field}_gmt FROM {$wpdb->posts} WHERE post_status = 'publish' AND post_type IN ({$post_types}) ORDER BY post_{$field}_gmt DESC LIMIT 1");
                break;
            case 'blog':
                $date = $wpdb->get_var("SELECT post_{$field} FROM {$wpdb->posts} WHERE post_status = 'publish' AND post_type IN ({$post_types}) ORDER BY post_{$field}_gmt DESC LIMIT 1");
                break;
            case 'server':
                $date = $wpdb->get_var("SELECT DATE_ADD(post_{$field}_gmt, INTERVAL '{$add_seconds_server}' SECOND) FROM {$wpdb->posts} WHERE post_status = 'publish' AND post_type IN ({$post_types}) ORDER BY post_{$field}_gmt DESC LIMIT 1");
                break;
        }
        if ($date) {
            wp_cache_set($key, $date, 'timeinfo');
        }
    }
    return $date;
}

WordPress Version: 4.1

/**
 * Get the timestamp of the last time any post was modified or published.
 *
 * @since 3.1.0
 * @access private
 *
 * @param string $timezone The timezone for the timestamp. See {@see get_lastpostmodified()}
 *                         for information on accepted values.
 * @param string $field    Post field to check. Accepts 'date' or 'modified'.
 * @return string The timestamp.
 */
function _get_last_post_time($timezone, $field)
{
    global $wpdb;
    if (!in_array($field, array('date', 'modified'))) {
        return false;
    }
    $timezone = strtolower($timezone);
    $key = "lastpost{$field}:{$timezone}";
    $date = wp_cache_get($key, 'timeinfo');
    if (!$date) {
        $add_seconds_server = date('Z');
        $post_types = get_post_types(array('public' => true));
        array_walk($post_types, array(&$wpdb, 'escape_by_ref'));
        $post_types = "'" . implode("', '", $post_types) . "'";
        switch ($timezone) {
            case 'gmt':
                $date = $wpdb->get_var("SELECT post_{$field}_gmt FROM {$wpdb->posts} WHERE post_status = 'publish' AND post_type IN ({$post_types}) ORDER BY post_{$field}_gmt DESC LIMIT 1");
                break;
            case 'blog':
                $date = $wpdb->get_var("SELECT post_{$field} FROM {$wpdb->posts} WHERE post_status = 'publish' AND post_type IN ({$post_types}) ORDER BY post_{$field}_gmt DESC LIMIT 1");
                break;
            case 'server':
                $date = $wpdb->get_var("SELECT DATE_ADD(post_{$field}_gmt, INTERVAL '{$add_seconds_server}' SECOND) FROM {$wpdb->posts} WHERE post_status = 'publish' AND post_type IN ({$post_types}) ORDER BY post_{$field}_gmt DESC LIMIT 1");
                break;
        }
        if ($date) {
            wp_cache_set($key, $date, 'timeinfo');
        }
    }
    return $date;
}

WordPress Version: 4.0

/**
 * Retrieve latest post date data based on timezone.
 *
 * @access private
 * @since 3.1.0
 *
 * @param string $timezone The location to get the time. Accepts 'gmt', 'blog', or 'server'.
 * @param string $field Field to check. Can be 'date' or 'modified'.
 * @return string The date.
 */
function _get_last_post_time($timezone, $field)
{
    global $wpdb;
    if (!in_array($field, array('date', 'modified'))) {
        return false;
    }
    $timezone = strtolower($timezone);
    $key = "lastpost{$field}:{$timezone}";
    $date = wp_cache_get($key, 'timeinfo');
    if (!$date) {
        $add_seconds_server = date('Z');
        $post_types = get_post_types(array('public' => true));
        array_walk($post_types, array(&$wpdb, 'escape_by_ref'));
        $post_types = "'" . implode("', '", $post_types) . "'";
        switch ($timezone) {
            case 'gmt':
                $date = $wpdb->get_var("SELECT post_{$field}_gmt FROM {$wpdb->posts} WHERE post_status = 'publish' AND post_type IN ({$post_types}) ORDER BY post_{$field}_gmt DESC LIMIT 1");
                break;
            case 'blog':
                $date = $wpdb->get_var("SELECT post_{$field} FROM {$wpdb->posts} WHERE post_status = 'publish' AND post_type IN ({$post_types}) ORDER BY post_{$field}_gmt DESC LIMIT 1");
                break;
            case 'server':
                $date = $wpdb->get_var("SELECT DATE_ADD(post_{$field}_gmt, INTERVAL '{$add_seconds_server}' SECOND) FROM {$wpdb->posts} WHERE post_status = 'publish' AND post_type IN ({$post_types}) ORDER BY post_{$field}_gmt DESC LIMIT 1");
                break;
        }
        if ($date) {
            wp_cache_set($key, $date, 'timeinfo');
        }
    }
    return $date;
}

WordPress Version: 3.7

/**
 * Retrieve latest post date data based on timezone.
 *
 * @access private
 * @since 3.1.0
 *
 * @param string $timezone The location to get the time. Can be 'gmt', 'blog', or 'server'.
 * @param string $field Field to check. Can be 'date' or 'modified'.
 * @return string The date.
 */
function _get_last_post_time($timezone, $field)
{
    global $wpdb;
    if (!in_array($field, array('date', 'modified'))) {
        return false;
    }
    $timezone = strtolower($timezone);
    $key = "lastpost{$field}:{$timezone}";
    $date = wp_cache_get($key, 'timeinfo');
    if (!$date) {
        $add_seconds_server = date('Z');
        $post_types = get_post_types(array('public' => true));
        array_walk($post_types, array(&$wpdb, 'escape_by_ref'));
        $post_types = "'" . implode("', '", $post_types) . "'";
        switch ($timezone) {
            case 'gmt':
                $date = $wpdb->get_var("SELECT post_{$field}_gmt FROM {$wpdb->posts} WHERE post_status = 'publish' AND post_type IN ({$post_types}) ORDER BY post_{$field}_gmt DESC LIMIT 1");
                break;
            case 'blog':
                $date = $wpdb->get_var("SELECT post_{$field} FROM {$wpdb->posts} WHERE post_status = 'publish' AND post_type IN ({$post_types}) ORDER BY post_{$field}_gmt DESC LIMIT 1");
                break;
            case 'server':
                $date = $wpdb->get_var("SELECT DATE_ADD(post_{$field}_gmt, INTERVAL '{$add_seconds_server}' SECOND) FROM {$wpdb->posts} WHERE post_status = 'publish' AND post_type IN ({$post_types}) ORDER BY post_{$field}_gmt DESC LIMIT 1");
                break;
        }
        if ($date) {
            wp_cache_set($key, $date, 'timeinfo');
        }
    }
    return $date;
}