Category query speedup and indentation fixes. Hat tip: Scott. http://mosquito.wordpress.org/view.php?id=1005

git-svn-id: https://develop.svn.wordpress.org/trunk@2396 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Matt Mullenweg 2005-03-01 09:10:12 +00:00
parent 0de297805a
commit a0eba624c5
2 changed files with 73 additions and 80 deletions

View File

@ -989,89 +989,84 @@ function get_posts($args) {
} }
function query_posts($query) { function query_posts($query) {
global $wp_query; global $wp_query;
return $wp_query->query($query);
return $wp_query->query($query);
} }
function update_post_caches($posts) { function update_post_caches($posts) {
global $category_cache, $comment_count_cache, $post_meta_cache; global $category_cache, $comment_count_cache, $post_meta_cache;
global $wpdb; 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. // Get the categories for all the posts
if (! $posts) { foreach ($posts as $post)
return; $post_id_list[] = $post->ID;
} $post_id_list = implode(',', $post_id_list);
// Get the categories for all the posts $dogs = $wpdb->get_results("SELECT DISTINCT
foreach ($posts as $post) post_id, category_id, cat_name, category_nicename, category_description, category_parent
$post_id_list[] = $post->ID; FROM $wpdb->categories, $wpdb->post2cat
$post_id_list = implode(',', $post_id_list); WHERE category_id = cat_ID AND post_id IN ($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");
if ($comment_counts) { if ( !empty($dogs) ) {
foreach ($comment_counts as $comment_count) { foreach ($dogs as $catt) {
$comment_count_cache["$comment_count->ID"] = $comment_count->ccount; $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 // 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) ) { 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:
// Change from flat structure to hierarchical: $post_meta_cache = array();
$post_meta_cache = array(); foreach ($meta_list as $metarow) {
foreach ($meta_list as $metarow) { $mpid = $metarow['post_id'];
$mpid = $metarow['post_id']; $mkey = $metarow['meta_key'];
$mkey = $metarow['meta_key']; $mval = $metarow['meta_value'];
$mval = $metarow['meta_value'];
// Force subkeys to be array type:
// Force subkeys to be array type: if (!isset($post_meta_cache[$mpid]) || !is_array($post_meta_cache[$mpid]))
if (!isset($post_meta_cache[$mpid]) || !is_array($post_meta_cache[$mpid])) $post_meta_cache[$mpid] = array();
$post_meta_cache[$mpid] = array(); if (!isset($post_meta_cache[$mpid]["$mkey"]) || !is_array($post_meta_cache[$mpid]["$mkey"]))
if (!isset($post_meta_cache[$mpid]["$mkey"]) || !is_array($post_meta_cache[$mpid]["$mkey"])) $post_meta_cache[$mpid]["$mkey"] = array();
$post_meta_cache[$mpid]["$mkey"] = array();
// Add a value to the current pid/key:
// Add a value to the current pid/key: $post_meta_cache[$mpid][$mkey][] = $mval;
$post_meta_cache[$mpid][$mkey][] = $mval; }
} }
}
} }
function update_category_cache() { function update_category_cache() {
global $cache_categories, $wpdb; global $cache_categories, $wpdb;
$dogs = $wpdb->get_results("SELECT * FROM $wpdb->categories"); $dogs = $wpdb->get_results("SELECT * FROM $wpdb->categories");
foreach ($dogs as $catt) { foreach ($dogs as $catt)
$cache_categories[$catt->cat_ID] = $catt; $cache_categories[$catt->cat_ID] = $catt;
}
} }
function update_user_cache() { function update_user_cache() {
global $cache_userdata, $wpdb; global $cache_userdata, $wpdb;
if ( $users = $wpdb->get_results("SELECT * FROM $wpdb->users WHERE user_level > 0") ) : if ( $users = $wpdb->get_results("SELECT * FROM $wpdb->users WHERE user_level > 0") ) :
foreach ($users as $user) : foreach ($users as $user) :
$cache_userdata[$user->ID] = $user; $cache_userdata[$user->ID] = $user;
$cache_userdata[$user->user_login] =& $cache_userdata[$user->ID]; $cache_userdata[$user->user_login] =& $cache_userdata[$user->ID];
endforeach; endforeach;
return true; return true;
else: else :
return false; return false;
endif; endif;
} }

View File

@ -3,27 +3,25 @@
function get_the_category($id = false) { function get_the_category($id = false) {
global $post, $wpdb, $category_cache; global $post, $wpdb, $category_cache;
if (! $id) { if ( !$id )
$id = $post->ID; $id = $post->ID;
}
if ($category_cache[$id]) { if ( $category_cache[$id] ) {
$categories = $category_cache[$id]; $categories = $category_cache[$id];
} else { } else {
$categories = $wpdb->get_results(" $categories = $wpdb->get_results("
SELECT category_id, cat_name, category_nicename, category_description, category_parent SELECT category_id, cat_name, category_nicename, category_description, category_parent
FROM $wpdb->categories, $wpdb->post2cat FROM $wpdb->categories, $wpdb->post2cat
WHERE $wpdb->post2cat.category_id = cat_ID AND $wpdb->post2cat.post_id = '$id' WHERE $wpdb->post2cat.category_id = cat_ID AND $wpdb->post2cat.post_id = '$id'
"); ");
} }
if (!empty($categories)) if (!empty($categories))
sort($categories); sort($categories);
else else
$categories = array(); $categories = array();
return $categories; return $categories;
} }
function get_category_link($category_id) { function get_category_link($category_id) {