WordPress.org

Make WordPress Core

Changes between Initial Version and Version 1 of Ticket #34560, comment 58


Ignore:
Timestamp:
06/29/2016 04:53:35 AM (3 years ago)
Author:
martijn-van-der-kooij
Comment:

Legend:

Unmodified
Added
Removed
Modified
  • Ticket #34560, comment 58

    initial v1  
    33@adamsilverstein yes, we are calling `wp_get_post_autosave` with no user id and this returns the last autosave that is made by any user.
    44
    5 A test could perform a autosave for several users and while being the first user use the function with no userid and get a the last saved one.
     5A test could perform a autosave for several users and while being the first user use the function with no userid and get the last saved one.
     6
     7I've adjusted your code here:
     8{{{#!php
     9    function swifty_get_post_autosave( $post_id, $user_id = 0 ) {
     10        global $wpdb;
     11
     12        // Construct the autosave query.
     13        $autosave_query = "
     14                SELECT *
     15                FROM $wpdb->posts
     16                WHERE post_parent = %d
     17                AND   post_type   = 'revision'
     18                AND   post_status = 'inherit'
     19                AND   post_name   = %s" .
     20            ( ( 0 !== $user_id ) ?
     21                "AND post_author = %d" : "" ) . "
     22                ORDER BY post_date DESC, ID DESC
     23                LIMIT 1";
     24
     25        $autosave_details = $wpdb->get_results(
     26            $wpdb->prepare(
     27                $autosave_query,
     28                $post_id,
     29                $post_id . '-autosave-v1',
     30                $user_id
     31            )
     32        );
     33
     34        if( empty( $autosave_details ) ) {
     35            return false;
     36        }
     37
     38        return $autosave_details[ 0 ];
     39    }
     40}}}
     41