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. |
| 5 | A 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 | |
| 7 | I'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 | |