Commit c88d343f8b for wordpress.org
commit c88d343f8bff5c6fc10c3f501c6468dd9cb61e3d
Author: peterwilsoncc <peterwilsoncc@git.wordpress.org>
Date: Thu Oct 1 23:20:49 2026 +0000
Canonical: Cache `redirect_guess_404_permalink()`.
Introduce caching of both hits and misses to the database query within `redirect_guess_404_permalink()`.
Props altf4falt, audrasjb, im3dabasia1, spacedmonkey, westonruter, wildworks.
Fixes #64250.
Built from https://develop.svn.wordpress.org/trunk@64043
git-svn-id: http://core.svn.wordpress.org/trunk@63202 1a063a9b-81f0-0310-95a4-ce76da25c4cd
diff --git a/wp-includes/canonical.php b/wp-includes/canonical.php
index 6faeab2c27..2b7553bda5 100644
--- a/wp-includes/canonical.php
+++ b/wp-includes/canonical.php
@@ -1007,8 +1007,22 @@ function redirect_guess_404_permalink() {
$where .= $wpdb->prepare( ' AND DAYOFMONTH(post_date) = %d', get_query_var( 'day' ) );
}
- // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
- $post_id = $wpdb->get_var( "SELECT ID FROM $wpdb->posts WHERE $where AND post_status IN ('" . implode( "', '", esc_sql( $publicly_viewable_statuses ) ) . "')" );
+ $query = "SELECT ID FROM $wpdb->posts WHERE $where AND post_status IN ('" . implode( "', '", esc_sql( $publicly_viewable_statuses ) ) . "')";
+
+ $key = md5( $query );
+ $last_changed = wp_cache_get_last_changed( 'posts' );
+ $cache_key = "redirect_guess_404_permalink:$key";
+ $cache = wp_cache_get_salted( $cache_key, 'post-queries', $last_changed );
+
+ if ( false !== $cache ) {
+ $post_id = $cache;
+ } else {
+ // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
+ $post_id = (int) $wpdb->get_var( $query );
+
+ // Cache misses as well as hits.
+ wp_cache_set_salted( $cache_key, $post_id, 'post-queries', $last_changed );
+ }
if ( ! $post_id ) {
return false;
diff --git a/wp-includes/version.php b/wp-includes/version.php
index 50b1606af1..7e54212881 100644
--- a/wp-includes/version.php
+++ b/wp-includes/version.php
@@ -16,7 +16,7 @@
*
* @global string $wp_version
*/
-$wp_version = '7.2-alpha-64042';
+$wp_version = '7.2-alpha-64043';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.