Skip to content

Commit

Permalink
Replaced raw query with WP_Query for wp_get_post_autosave
Browse files Browse the repository at this point in the history
  • Loading branch information
narenin committed Dec 6, 2024
1 parent 411d669 commit 16533fd
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions src/wp-includes/revision.php
Original file line number Diff line number Diff line change
Expand Up @@ -277,35 +277,35 @@ function wp_save_post_revision( $post_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 ) {
// Set up the query args for WP_Query
// Set up the query args for WP_Query.
$args = array(
'post_type' => 'revision',
'post_status' => 'inherit',
'post_parent' => $post_id,
'post_name' => $post_id . '-autosave-v1', // Autosave name
'posts_per_page' => 1, // Only need the latest autosave
'orderby' => 'date', // Order by post date (latest first)
'order' => 'DESC', // Descending order for latest
'fields' => 'ids', // We only need the post ID to fetch the post object
'post_name' => $post_id . '-autosave-v1', // Autosave name.
'posts_per_page' => 1, // Only need the latest autosave.
'orderby' => 'date', // Order by post date (latest first).
'order' => 'DESC', // Descending order for latest.
'fields' => 'ids', // We only need the post ID to fetch the post object.
);

// If user_id is set, we add the author parameter to the query.
if ( $user_id !== 0 ) {

Check failure on line 293 in src/wp-includes/revision.php

View workflow job for this annotation

GitHub Actions / PHP coding standards / Run coding standards checks

Use Yoda Condition checks, you must.
$args['author'] = $user_id;
}

// Run the query to get autosave
// Run the query to get autosave.
$query = new WP_Query( $args );

// If no autosave found, return false
// If no autosave found, return false.
if ( ! $query->have_posts() ) {
return false;
}

// Get the first post (which is the latest autosave)
// Get the first post (which is the latest autosave).
$autosave_id = $query->posts[0];

// Return the post object
// Return the post object.
return get_post( $autosave_id );
}

Expand Down

0 comments on commit 16533fd

Please sign in to comment.