From 830907b08d48c617d66fe7bc50b841744063a6f5 Mon Sep 17 00:00:00 2001 From: Ryan Boren Date: Fri, 8 May 2009 18:04:13 +0000 Subject: [PATCH] Cache get_adjacent_post() queries for the duration of the page load to avoid running queries multiple times git-svn-id: https://develop.svn.wordpress.org/trunk@11241 602fd350-edb4-49c9-b593-d223f7449a82 --- wp-includes/link-template.php | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/wp-includes/link-template.php b/wp-includes/link-template.php index 3ba099bdc1..14d7e8fc89 100644 --- a/wp-includes/link-template.php +++ b/wp-includes/link-template.php @@ -868,7 +868,7 @@ function get_next_post($in_same_cat = false, $excluded_categories = '') { function get_adjacent_post($in_same_cat = false, $excluded_categories = '', $previous = true) { global $post, $wpdb; - if( empty($post) || !is_single() || is_attachment() ) + if ( empty($post) || !is_single() || is_attachment() ) return null; $current_post_date = $post->post_date; @@ -905,7 +905,15 @@ function get_adjacent_post($in_same_cat = false, $excluded_categories = '', $pre $where = apply_filters( "get_{$adjacent}_post_where", $wpdb->prepare("WHERE p.post_date $op %s AND p.post_type = 'post' AND p.post_status = 'publish' $posts_in_ex_cats_sql", $current_post_date), $in_same_cat, $excluded_categories ); $sort = apply_filters( "get_{$adjacent}_post_sort", "ORDER BY p.post_date $order LIMIT 1" ); - return $wpdb->get_row("SELECT p.* FROM $wpdb->posts AS p $join $where $sort"); + $query = "SELECT p.* FROM $wpdb->posts AS p $join $where $sort"; + $query_key = 'adjacent_post_' . md5($query); + $result = wp_cache_get($query_key, 'counts'); + if ( false !== $result ) + return $result; + + $result = $wpdb->get_row("SELECT p.* FROM $wpdb->posts AS p $join $where $sort"); + wp_cache_set($query_key, $result, 'counts'); + return $result; } /**