From 16533fd542b59747866964b23d95481b429a2740 Mon Sep 17 00:00:00 2001 From: narenin Date: Fri, 6 Dec 2024 14:40:49 +0530 Subject: [PATCH] Replaced raw query with WP_Query for wp_get_post_autosave --- src/wp-includes/revision.php | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/wp-includes/revision.php b/src/wp-includes/revision.php index 626a488f87ef4..e1e5f9159f379 100644 --- a/src/wp-includes/revision.php +++ b/src/wp-includes/revision.php @@ -277,16 +277,16 @@ 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. @@ -294,18 +294,18 @@ function wp_get_post_autosave( $post_id, $user_id = 0 ) { $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 ); }