diff --git a/wp-includes/functions.php b/wp-includes/functions.php index 4453edb3a7..2adf42c848 100644 --- a/wp-includes/functions.php +++ b/wp-includes/functions.php @@ -546,21 +546,34 @@ function clean_page_cache($id) { } function update_post_category_cache($post_ids) { - global $wpdb, $category_cache; + global $wpdb, $category_cache, $blog_id; if ( empty($post_ids) ) return; if ( is_array($post_ids) ) - $post_ids = implode(',', $post_ids); + $post_id_list = implode(',', $post_ids); - $dogs = $wpdb->get_results("SELECT post_id, category_id FROM $wpdb->post2cat WHERE post_id IN ($post_ids)"); + $post_id_array = (array) explode(',', $post_ids); + $count = count( $post_id_array); + for ( $i = 0; $i < $count; $i++ ) { + $post_id = $post_id_array[ $i ]; + if ( isset( $category_cache[$blog_id][$post_id] ) ) { + unset( $post_id_array[ $i ] ); + continue; + } + } + if ( count( $post_id_array ) == 0 ) + return; + $post_id_list = join( ',', $post_id_array ); // with already cached stuff removed + + $dogs = $wpdb->get_results("SELECT post_id, category_id FROM $wpdb->post2cat WHERE post_id IN ($post_id_list)"); if ( empty($dogs) ) return; foreach ($dogs as $catt) - $category_cache[$catt->post_id][$catt->category_id] = &get_category($catt->category_id); + $category_cache[$blog_id][$catt->post_id][$catt->category_id] = &get_category($catt->category_id); } function update_post_caches(&$posts) { @@ -590,11 +603,24 @@ function update_postmeta_cache($post_id_list = '') { // We should validate this comma-separated list for the upcoming SQL query $post_id_list = preg_replace('|[^0-9,]|', '', $post_id_list); + if ( empty( $post_id_list ) ) + return false; + // we're marking each post as having its meta cached (with no keys... empty array), to prevent posts with no meta keys from being queried again // any posts that DO have keys will have this empty array overwritten with a proper array, down below - $post_id_array = explode(',', $post_id_list); - foreach ( (array) $post_id_array as $pid ) - $post_meta_cache[$pid] = array(); + $post_id_array = (array) explode(',', $post_id_list); + $count = count( $post_id_array); + for ( $i = 0; $i < $count; $i++ ) { + $post_id = $post_id_array[ $i ]; + if ( isset( $post_meta_cache[$blog_id][$post_id] ) ) { // If the meta is already cached + unset( $post_id_array[ $i ] ); + continue; + } + $post_meta_cache[$blog_id][$post_id] = array(); + } + if ( count( $post_id_array ) == 0 ) + return; + $post_id_list = join( ',', $post_id_array ); // with already cached stuff removeds // Get post-meta info if ( $meta_list = $wpdb->get_results("SELECT post_id, meta_key, meta_value FROM $wpdb->postmeta WHERE post_id IN($post_id_list) ORDER BY post_id, meta_key", ARRAY_A) ) { diff --git a/wp-includes/query.php b/wp-includes/query.php index 8c95cbcd75..aaab66f1c1 100644 --- a/wp-includes/query.php +++ b/wp-includes/query.php @@ -1012,9 +1012,10 @@ class WP_Query { } } + $this->posts = apply_filters('the_posts', $this->posts); + update_post_caches($this->posts); - $this->posts = apply_filters('the_posts', $this->posts); $this->post_count = count($this->posts); if ($this->post_count > 0) { $this->post = $this->posts[0];