From 06d61e1f7a9b95c150589e70d468d4d25a79b480 Mon Sep 17 00:00:00 2001 From: Ryan Boren Date: Tue, 15 Nov 2005 16:31:24 +0000 Subject: [PATCH] Update category counts. Props donncha. fixes #1886 git-svn-id: https://develop.svn.wordpress.org/trunk@3091 602fd350-edb4-49c9-b593-d223f7449a82 --- wp-admin/upgrade-functions.php | 4 ++-- wp-includes/functions-post.php | 18 ++++++++++++++++++ wp-includes/version.php | 2 +- 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/wp-admin/upgrade-functions.php b/wp-admin/upgrade-functions.php index 3ad6d9bbca..9492d40d9f 100644 --- a/wp-admin/upgrade-functions.php +++ b/wp-admin/upgrade-functions.php @@ -30,7 +30,7 @@ function upgrade_all() { upgrade_130(); } - if ( $wp_current_db_version < 2966 ) + if ( $wp_current_db_version < 3091 ) upgrade_160(); save_mod_rewrite_rules(); @@ -296,7 +296,7 @@ function upgrade_160() { if ( 0 == $wpdb->get_var("SELECT SUM(category_count) FROM $wpdb->categories") ) { // Create counts $categories = $wpdb->get_col("SELECT cat_ID FROM $wpdb->categories"); foreach ( $categories as $cat_id ) { - $count = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->post2cat WHERE category_id = '$cat_id'"); + $count = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->post2cat, $wpdb->posts WHERE $wpdb->posts.ID=$wpdb->post2cat.post_id AND post_status='publish' AND category_id = '$cat_id'"); $wpdb->query("UPDATE $wpdb->categories SET category_count = '$count' WHERE cat_ID = '$cat_id'"); } } diff --git a/wp-includes/functions-post.php b/wp-includes/functions-post.php index 14c8f8606a..4fa6baabdc 100644 --- a/wp-includes/functions-post.php +++ b/wp-includes/functions-post.php @@ -167,6 +167,14 @@ function wp_insert_post($postarr = array()) { if ($post_status == 'publish') { do_action('publish_post', $post_ID); + + // Update category counts. + foreach ( $post_category as $cat_id ) { + $count = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->post2cat, $wpdb->posts WHERE $wpdb->posts.ID=$wpdb->post2cat.post_id AND post_status='publish' AND category_id = '$cat_id'"); + $wpdb->query("UPDATE $wpdb->categories SET category_count = '$count' WHERE cat_ID = '$cat_id'"); + wp_cache_delete($cat_id, 'category'); + } + if ($post_pingback && !defined('WP_IMPORTING')) $result = $wpdb->query(" INSERT INTO $wpdb->postmeta @@ -477,6 +485,16 @@ function wp_delete_post($postid = 0) { do_action('delete_post', $postid); + if ( 'publish' == $post->post_status) { + $categories = wp_get_post_cats('', $post->ID); + if( is_array( $categories ) ) { + foreach ( $categories as $cat_id ) { + $wpdb->query("UPDATE $wpdb->categories SET category_count = category_count - 1 WHERE cat_ID = '$cat_id'"); + wp_cache_delete($cat_id, 'category'); + } + } + } + if ( 'static' == $post->post_status ) $wpdb->query("UPDATE $wpdb->posts SET post_parent = $post->post_parent WHERE post_parent = $postid AND post_status = 'static'"); diff --git a/wp-includes/version.php b/wp-includes/version.php index 01ac776764..789eeef61d 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -3,6 +3,6 @@ // This just holds the version number, in a separate file so we can bump it without cluttering the SVN $wp_version = '1.6-ALPHA-2-still-dont-use'; -$wp_db_version = 2966; +$wp_db_version = 3091; ?> \ No newline at end of file