From 2ebc1fb14304e564c5e83fb84d8f7551c225d143 Mon Sep 17 00:00:00 2001 From: Boone Gorges Date: Thu, 22 Jun 2017 03:19:44 +0000 Subject: [PATCH] Cache results in `get_objects_in_term()`. This helps to reduce database queries when generating nav menus. Props spacedmonkey. Fixes #37094. Built from https://develop.svn.wordpress.org/trunk@40921 git-svn-id: http://core.svn.wordpress.org/trunk@40771 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/taxonomy.php | 12 +++++++++++- wp-includes/version.php | 2 +- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/wp-includes/taxonomy.php b/wp-includes/taxonomy.php index f703b5a56b..1a0d170848 100644 --- a/wp-includes/taxonomy.php +++ b/wp-includes/taxonomy.php @@ -650,7 +650,17 @@ function get_objects_in_term( $term_ids, $taxonomies, $args = array() ) { $taxonomies = "'" . implode( "', '", array_map( 'esc_sql', $taxonomies ) ) . "'"; $term_ids = "'" . implode( "', '", $term_ids ) . "'"; - $object_ids = $wpdb->get_col("SELECT tr.object_id FROM $wpdb->term_relationships AS tr INNER JOIN $wpdb->term_taxonomy AS tt ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE tt.taxonomy IN ($taxonomies) AND tt.term_id IN ($term_ids) ORDER BY tr.object_id $order"); + $sql = "SELECT tr.object_id FROM $wpdb->term_relationships AS tr INNER JOIN $wpdb->term_taxonomy AS tt ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE tt.taxonomy IN ($taxonomies) AND tt.term_id IN ($term_ids) ORDER BY tr.object_id $order"; + + $last_changed = wp_cache_get_last_changed( 'terms' ); + $cache_key = 'get_objects_in_term:' . md5( $sql ) . ":$last_changed"; + $cache = wp_cache_get( $cache_key, 'terms' ); + if ( false === $cache ) { + $object_ids = $wpdb->get_col( $sql ); + wp_cache_set( $cache_key, $object_ids, 'terms' ); + } else { + $object_ids = (array) $cache; + } if ( ! $object_ids ){ return array(); diff --git a/wp-includes/version.php b/wp-includes/version.php index c3f8190348..cbf0b346ad 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -4,7 +4,7 @@ * * @global string $wp_version */ -$wp_version = '4.9-alpha-40920'; +$wp_version = '4.9-alpha-40921'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.