Skip to content

Commit 88848eb

Browse files
committed
Posts, Post Types: Use persistent caching in get_adjacent_post function.
The function `get_adjacent_post` cached the results of database query in the cache group `counts`. This is a none persistent group and meant cache would not persist on the next request. Change cache to save to the `posts` cache group. Cache invalidation is done by using get last changed value of the `posts` and `terms` group as a salt for the cache key. Props spacedmonkey, peterwilsoncc, johnbillion, boonebgorges, mukesh27, dd32. Fixes #41131. Built from https://develop.svn.wordpress.org/trunk@55085 git-svn-id: http://core.svn.wordpress.org/trunk@54618 1a063a9b-81f0-0310-95a4-ce76da25c4cd
1 parent c2c1c75 commit 88848eb

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

wp-includes/link-template.php

+10-4
Original file line numberDiff line numberDiff line change
@@ -1976,9 +1976,15 @@ function get_adjacent_post( $in_same_term = false, $excluded_terms = '', $previo
19761976
*/
19771977
$sort = apply_filters( "get_{$adjacent}_post_sort", "ORDER BY p.post_date $order LIMIT 1", $post, $order );
19781978

1979-
$query = "SELECT p.ID FROM $wpdb->posts AS p $join $where $sort";
1980-
$query_key = 'adjacent_post_' . md5( $query );
1981-
$result = wp_cache_get( $query_key, 'counts' );
1979+
$query = "SELECT p.ID FROM $wpdb->posts AS p $join $where $sort";
1980+
$key = md5( $query );
1981+
$last_changed = wp_cache_get_last_changed( 'posts' );
1982+
if ( $in_same_term || ! empty( $excluded_terms ) ) {
1983+
$last_changed .= wp_cache_get_last_changed( 'terms' );
1984+
}
1985+
$cache_key = "adjacent_post:$key:$last_changed";
1986+
1987+
$result = wp_cache_get( $cache_key, 'posts' );
19821988
if ( false !== $result ) {
19831989
if ( $result ) {
19841990
$result = get_post( $result );
@@ -1991,7 +1997,7 @@ function get_adjacent_post( $in_same_term = false, $excluded_terms = '', $previo
19911997
$result = '';
19921998
}
19931999

1994-
wp_cache_set( $query_key, $result, 'counts' );
2000+
wp_cache_set( $cache_key, $result, 'posts' );
19952001

19962002
if ( $result ) {
19972003
$result = get_post( $result );

wp-includes/version.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
*
1717
* @global string $wp_version
1818
*/
19-
$wp_version = '6.2-alpha-55084';
19+
$wp_version = '6.2-alpha-55085';
2020

2121
/**
2222
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.

0 commit comments

Comments
 (0)