From 2233bfb700a269738a64ce7218ad4f7e3178aa41 Mon Sep 17 00:00:00 2001 From: matt Date: Tue, 1 Mar 2005 09:10:12 +0000 Subject: [PATCH] Category query speedup and indentation fixes. Hat tip: Scott. http://mosquito.wordpress.org/view.php?id=1005 git-svn-id: http://svn.automattic.com/wordpress/trunk@2396 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/functions.php | 123 ++++++++++---------- wp-includes/template-functions-category.php | 30 +++-- 2 files changed, 73 insertions(+), 80 deletions(-) diff --git a/wp-includes/functions.php b/wp-includes/functions.php index bc62a89019..5a9d790308 100644 --- a/wp-includes/functions.php +++ b/wp-includes/functions.php @@ -989,89 +989,84 @@ function get_posts($args) { } function query_posts($query) { - global $wp_query; - - return $wp_query->query($query); + global $wp_query; + return $wp_query->query($query); } function update_post_caches($posts) { - global $category_cache, $comment_count_cache, $post_meta_cache; - global $wpdb; + global $category_cache, $comment_count_cache, $post_meta_cache; + global $wpdb; + + // No point in doing all this work if we didn't match any posts. + if ( !$posts ) + return; - // No point in doing all this work if we didn't match any posts. - if (! $posts) { - return; - } + // Get the categories for all the posts + foreach ($posts as $post) + $post_id_list[] = $post->ID; + $post_id_list = implode(',', $post_id_list); - // 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, category_parent - FROM $wpdb->categories, $wpdb->post2cat, $wpdb->posts - WHERE category_id = cat_ID AND post_id = ID AND post_id IN ($post_id_list)"); - - if (!empty($dogs)) { - foreach ($dogs as $catt) { - $category_cache[$catt->ID][$catt->category_id] = $catt; - } - } - - // Do the same for comment numbers - $comment_counts = $wpdb->get_results("SELECT ID, COUNT( comment_ID ) AS ccount - FROM $wpdb->posts - LEFT JOIN $wpdb->comments ON ( comment_post_ID = ID AND comment_approved = '1') - WHERE post_status = 'publish' AND ID IN ($post_id_list) - GROUP BY ID"); + $dogs = $wpdb->get_results("SELECT DISTINCT + post_id, category_id, cat_name, category_nicename, category_description, category_parent + FROM $wpdb->categories, $wpdb->post2cat + WHERE category_id = cat_ID AND post_id IN ($post_id_list)"); - if ($comment_counts) { - foreach ($comment_counts as $comment_count) { - $comment_count_cache["$comment_count->ID"] = $comment_count->ccount; - } - } + if ( !empty($dogs) ) { + foreach ($dogs as $catt) { + $category_cache[$catt->post_id][$catt->category_id] = $catt; + } + } + + // Do the same for comment numbers + $comment_counts = $wpdb->get_results("SELECT ID, COUNT( comment_ID ) AS ccount + FROM $wpdb->posts + LEFT JOIN $wpdb->comments ON ( comment_post_ID = ID AND comment_approved = '1') + WHERE post_status = 'publish' AND ID IN ($post_id_list) + GROUP BY ID"); + + if ($comment_counts) { + foreach ($comment_counts as $comment_count) + $comment_count_cache["$comment_count->ID"] = $comment_count->ccount; + } // 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) ) { - - // Change from flat structure to hierarchical: - $post_meta_cache = array(); - foreach ($meta_list as $metarow) { - $mpid = $metarow['post_id']; - $mkey = $metarow['meta_key']; - $mval = $metarow['meta_value']; - - // Force subkeys to be array type: - if (!isset($post_meta_cache[$mpid]) || !is_array($post_meta_cache[$mpid])) - $post_meta_cache[$mpid] = array(); - if (!isset($post_meta_cache[$mpid]["$mkey"]) || !is_array($post_meta_cache[$mpid]["$mkey"])) - $post_meta_cache[$mpid]["$mkey"] = array(); - - // Add a value to the current pid/key: - $post_meta_cache[$mpid][$mkey][] = $mval; - } - } + 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) ) { + // Change from flat structure to hierarchical: + $post_meta_cache = array(); + foreach ($meta_list as $metarow) { + $mpid = $metarow['post_id']; + $mkey = $metarow['meta_key']; + $mval = $metarow['meta_value']; + + // Force subkeys to be array type: + if (!isset($post_meta_cache[$mpid]) || !is_array($post_meta_cache[$mpid])) + $post_meta_cache[$mpid] = array(); + if (!isset($post_meta_cache[$mpid]["$mkey"]) || !is_array($post_meta_cache[$mpid]["$mkey"])) + $post_meta_cache[$mpid]["$mkey"] = array(); + + // Add a value to the current pid/key: + $post_meta_cache[$mpid][$mkey][] = $mval; + } + } } function update_category_cache() { - global $cache_categories, $wpdb; - $dogs = $wpdb->get_results("SELECT * FROM $wpdb->categories"); - foreach ($dogs as $catt) { - $cache_categories[$catt->cat_ID] = $catt; - } + global $cache_categories, $wpdb; + $dogs = $wpdb->get_results("SELECT * FROM $wpdb->categories"); + foreach ($dogs as $catt) + $cache_categories[$catt->cat_ID] = $catt; } function update_user_cache() { - global $cache_userdata, $wpdb; - - if ( $users = $wpdb->get_results("SELECT * FROM $wpdb->users WHERE user_level > 0") ) : + global $cache_userdata, $wpdb; + + if ( $users = $wpdb->get_results("SELECT * FROM $wpdb->users WHERE user_level > 0") ) : foreach ($users as $user) : $cache_userdata[$user->ID] = $user; $cache_userdata[$user->user_login] =& $cache_userdata[$user->ID]; endforeach; return true; - else: + else : return false; endif; } diff --git a/wp-includes/template-functions-category.php b/wp-includes/template-functions-category.php index 82a9267847..ab2331d6a3 100644 --- a/wp-includes/template-functions-category.php +++ b/wp-includes/template-functions-category.php @@ -3,27 +3,25 @@ function get_the_category($id = false) { global $post, $wpdb, $category_cache; - if (! $id) { - $id = $post->ID; - } + if ( !$id ) + $id = $post->ID; - if ($category_cache[$id]) { - $categories = $category_cache[$id]; + if ( $category_cache[$id] ) { + $categories = $category_cache[$id]; } else { - $categories = $wpdb->get_results(" - SELECT category_id, cat_name, category_nicename, category_description, category_parent - FROM $wpdb->categories, $wpdb->post2cat - WHERE $wpdb->post2cat.category_id = cat_ID AND $wpdb->post2cat.post_id = '$id' - "); - + $categories = $wpdb->get_results(" + SELECT category_id, cat_name, category_nicename, category_description, category_parent + FROM $wpdb->categories, $wpdb->post2cat + WHERE $wpdb->post2cat.category_id = cat_ID AND $wpdb->post2cat.post_id = '$id' + "); } - if (!empty($categories)) - sort($categories); - else - $categories = array(); + if (!empty($categories)) + sort($categories); + else + $categories = array(); - return $categories; + return $categories; } function get_category_link($category_id) {