From a8b5faacdce41f6186c9d185c5a7c97acfa3c3d8 Mon Sep 17 00:00:00 2001 From: saxmatt Date: Tue, 6 Jan 2004 12:44:46 +0000 Subject: [PATCH] Reduce queries. git-svn-id: http://svn.automattic.com/wordpress/trunk@727 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-blog-header.php | 20 ++++++++++++++++++++ wp-includes/template-functions.php | 20 ++++++++++++-------- 2 files changed, 32 insertions(+), 8 deletions(-) diff --git a/wp-blog-header.php b/wp-blog-header.php index 07234917b7..6d6433a4bc 100644 --- a/wp-blog-header.php +++ b/wp-blog-header.php @@ -167,6 +167,7 @@ if ((empty($cat)) || ($cat == 'all') || ($cat == '0')) { if ('' != $category_name) { $category_name = preg_replace('|[^a-z0-9-/]|', '', $category_name); + $tables = ", $tablepost2cat, $tablecategories"; $join = " LEFT JOIN $tablepost2cat ON ($tableposts.ID = $tablepost2cat.post_id) LEFT JOIN $tablecategories ON ($tablepost2cat.category_id = $tablecategories.cat_ID) "; $whichcat = " AND (category_nicename = '$category_name') "; $cat = $wpdb->get_var("SELECT cat_ID FROM $tablecategories WHERE category_nicename = '$category_name'"); @@ -319,6 +320,25 @@ if ($preview) { // echo $request; $posts = $wpdb->get_results($request); + +// Get the categories for all the posts +foreach ($posts as $post) { + $post_id_list[] = $post->ID; +} +$post_id_list = implode(',', $post_id_list); + +$dogs = $wpdb->get_results("SELECT DISTINCT + ID, category_id, cat_name, category_nicename, category_description + FROM $tablecategories, $tablepost2cat, $tableposts + WHERE category_id = cat_ID AND post_id = ID AND post_id IN ($post_id_list)"); + +foreach ($dogs as $catt) { + $category_cache[$catt->ID][] = $catt; +} + +// Do the same for comment numbers + + if (1 == count($posts)) { if ($p || $name) { $more = 1; diff --git a/wp-includes/template-functions.php b/wp-includes/template-functions.php index 4967d6d1dc..c314d156d0 100644 --- a/wp-includes/template-functions.php +++ b/wp-includes/template-functions.php @@ -1332,14 +1332,18 @@ function posts_nav_link($sep=' :: ', $prelabel='<< Previous Page', $nxtlabel='Ne /***** Category tags *****/ function get_the_category() { - global $post, $tablecategories, $tablepost2cat, $wpdb; - $categories = $wpdb->get_results(" - SELECT category_id, cat_name, category_nicename, category_description - FROM $tablecategories, $tablepost2cat - WHERE $tablepost2cat.category_id = cat_ID AND $tablepost2cat.post_id = $post->ID - "); - - return $categories; + global $post, $tablecategories, $tablepost2cat, $wpdb, $category_cache; + if ($category_cache[$post->ID]) { + return $category_cache[$post->ID]; + } else { + $categories = $wpdb->get_results(" + SELECT category_id, cat_name, category_nicename, category_description + FROM $tablecategories, $tablepost2cat + WHERE $tablepost2cat.category_id = cat_ID AND $tablepost2cat.post_id = $post->ID + "); + + return $categories; + } } function get_category_link($echo = false, $category_id, $category_nicename) {