wp_get_post_autosave

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

WordPress Version: 6.2

/**
 * Retrieves the autosaved data of the specified post.
 *
 * Returns a post object with the information that was autosaved for the specified post.
 * If the optional $user_id is passed, returns the autosave for that user, otherwise
 * returns the latest autosave.
 *
 * @since 2.6.0
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @param int $post_id The post ID.
 * @param int $user_id Optional. The post author ID. Default 0.
 * @return WP_Post|false The autosaved data or false on failure or when no autosave exists.
 */
function wp_get_post_autosave($post_id, $user_id = 0)
{
    global $wpdb;
    $autosave_name = $post_id . '-autosave-v1';
    $user_id_query = (0 !== $user_id) ? "AND post_author = {$user_id}" : null;
    // Construct the autosave query.
    $autosave_query = "\n\t\tSELECT *\n\t\tFROM {$wpdb->posts}\n\t\tWHERE post_parent = %d\n\t\tAND post_type = 'revision'\n\t\tAND post_status = 'inherit'\n\t\tAND post_name   = %s " . $user_id_query . '
		ORDER BY post_date DESC
		LIMIT 1';
    $autosave = $wpdb->get_results($wpdb->prepare($autosave_query, $post_id, $autosave_name));
    if (!$autosave) {
        return false;
    }
    return get_post($autosave[0]);
}

WordPress Version: 6.1

/**
 * Retrieves the autosaved data of the specified post.
 *
 * Returns a post object with the information that was autosaved for the specified post.
 * If the optional $user_id is passed, returns the autosave for that user, otherwise
 * returns the latest autosave.
 *
 * @since 2.6.0
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @param int $post_id The post ID.
 * @param int $user_id Optional. The post author ID.
 * @return WP_Post|false The autosaved data or false on failure or when no autosave exists.
 */
function wp_get_post_autosave($post_id, $user_id = 0)
{
    global $wpdb;
    $autosave_name = $post_id . '-autosave-v1';
    $user_id_query = (0 !== $user_id) ? "AND post_author = {$user_id}" : null;
    // Construct the autosave query.
    $autosave_query = "\n\t\tSELECT *\n\t\tFROM {$wpdb->posts}\n\t\tWHERE post_parent = %d\n\t\tAND post_type = 'revision'\n\t\tAND post_status = 'inherit'\n\t\tAND post_name   = %s " . $user_id_query . '
		ORDER BY post_date DESC
		LIMIT 1';
    $autosave = $wpdb->get_results($wpdb->prepare($autosave_query, $post_id, $autosave_name));
    if (!$autosave) {
        return false;
    }
    return get_post($autosave[0]);
}

WordPress Version: 5.6

/**
 * Retrieve the autosaved data of the specified post.
 *
 * Returns a post object with the information that was autosaved for the specified post.
 * If the optional $user_id is passed, returns the autosave for that user, otherwise
 * returns the latest autosave.
 *
 * @since 2.6.0
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @param int $post_id The post ID.
 * @param int $user_id Optional The post author ID.
 * @return WP_Post|false The autosaved data or false on failure or when no autosave exists.
 */
function wp_get_post_autosave($post_id, $user_id = 0)
{
    global $wpdb;
    $autosave_name = $post_id . '-autosave-v1';
    $user_id_query = (0 !== $user_id) ? "AND post_author = {$user_id}" : null;
    // Construct the autosave query.
    $autosave_query = "\n\t\tSELECT *\n\t\tFROM {$wpdb->posts}\n\t\tWHERE post_parent = %d\n\t\tAND post_type = 'revision'\n\t\tAND post_status = 'inherit'\n\t\tAND post_name   = %s " . $user_id_query . '
		ORDER BY post_date DESC
		LIMIT 1';
    $autosave = $wpdb->get_results($wpdb->prepare($autosave_query, $post_id, $autosave_name));
    if (!$autosave) {
        return false;
    }
    return get_post($autosave[0]);
}

WordPress Version: 5.5

/**
 * Retrieve the autosaved data of the specified post.
 *
 * Returns a post object with the information that was autosaved for the specified post.
 * If the optional $user_id is passed, returns the autosave for that user, otherwise
 * returns the latest autosave.
 *
 * @since 2.6.0
 *
 * @param int $post_id The post ID.
 * @param int $user_id Optional The post author ID.
 * @return WP_Post|false The autosaved data or false on failure or when no autosave exists.
 */
function wp_get_post_autosave($post_id, $user_id = 0)
{
    global $wpdb;
    $autosave_name = $post_id . '-autosave-v1';
    $user_id_query = (0 !== $user_id) ? "AND post_author = {$user_id}" : null;
    // Construct the autosave query.
    $autosave_query = "\n\t\tSELECT *\n\t\tFROM {$wpdb->posts}\n\t\tWHERE post_parent = %d\n\t\tAND post_type = 'revision'\n\t\tAND post_status = 'inherit'\n\t\tAND post_name   = %s " . $user_id_query . '
		ORDER BY post_date DESC
		LIMIT 1';
    $autosave = $wpdb->get_results($wpdb->prepare($autosave_query, $post_id, $autosave_name));
    if (!$autosave) {
        return false;
    }
    return get_post($autosave[0]);
}

WordPress Version: 4.3

/**
 * Retrieve the autosaved data of the specified post.
 *
 * Returns a post object containing the information that was autosaved for the
 * specified post. If the optional $user_id is passed, returns the autosave for that user
 * otherwise returns the latest autosave.
 *
 * @since 2.6.0
 *
 * @param int $post_id The post ID.
 * @param int $user_id Optional The post author ID.
 * @return WP_Post|false The autosaved data or false on failure or when no autosave exists.
 */
function wp_get_post_autosave($post_id, $user_id = 0)
{
    $revisions = wp_get_post_revisions($post_id, array('check_enabled' => false));
    foreach ($revisions as $revision) {
        if (false !== strpos($revision->post_name, "{$post_id}-autosave")) {
            if ($user_id && $user_id != $revision->post_author) {
                continue;
            }
            return $revision;
        }
    }
    return false;
}

WordPress Version: 4.1

/**
 * Retrieve the autosaved data of the specified post.
 *
 * Returns a post object containing the information that was autosaved for the
 * specified post. If the optional $user_id is passed, returns the autosave for that user
 * otherwise returns the latest autosave.
 *
 * @since 2.6.0
 *
 * @param int $post_id The post ID.
 * @param int $user_id optional The post author ID.
 * @return object|bool The autosaved data or false on failure or when no autosave exists.
 */
function wp_get_post_autosave($post_id, $user_id = 0)
{
    $revisions = wp_get_post_revisions($post_id, array('check_enabled' => false));
    foreach ($revisions as $revision) {
        if (false !== strpos($revision->post_name, "{$post_id}-autosave")) {
            if ($user_id && $user_id != $revision->post_author) {
                continue;
            }
            return $revision;
        }
    }
    return false;
}

WordPress Version: 4.0

/**
 * Retrieve the autosaved data of the specified post.
 *
 * Returns a post object containing the information that was autosaved for the
 * specified post. If the optional $user_id is passed, returns the autosave for that user
 * otherwise returns the latest autosave.
 *
 * @since 2.6.0
 *
 * @uses wp_get_post_revisions()
 *
 * @param int $post_id The post ID.
 * @param int $user_id optional The post author ID.
 * @return object|bool The autosaved data or false on failure or when no autosave exists.
 */
function wp_get_post_autosave($post_id, $user_id = 0)
{
    $revisions = wp_get_post_revisions($post_id, array('check_enabled' => false));
    foreach ($revisions as $revision) {
        if (false !== strpos($revision->post_name, "{$post_id}-autosave")) {
            if ($user_id && $user_id != $revision->post_author) {
                continue;
            }
            return $revision;
        }
    }
    return false;
}

WordPress Version: 3.7

/**
 * Retrieve the autosaved data of the specified post.
 *
 * Returns a post object containing the information that was autosaved for the
 * specified post. If the optional $user_id is passed, returns the autosave for that user
 * otherwise returns the latest autosave.
 *
 * @since 2.6.0
 *
 * @uses wp_get_post_revisions()
 *
 * @param int $post_id The post ID.
 * @param int $user_id optional The post author ID.
 * @return object|bool The autosaved data or false on failure or when no autosave exists.
 */
function wp_get_post_autosave($post_id, $user_id = 0)
{
    $revisions = wp_get_post_revisions($post_id, array('check_enabled' => false));
    foreach ($revisions as $revision) {
        if (false !== strpos($revision->post_name, "{$post_id}-autosave")) {
            if ($user_id && $user_id != $revision->post_author) {
                continue;
            }
            return $revision;
            break;
        }
    }
    return false;
}