Update category counts. Props donncha. fixes #1886

git-svn-id: http://svn.automattic.com/wordpress/trunk@3091 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
ryan 2005-11-15 16:31:24 +00:00
parent 9ba3193309
commit e120711bdd
3 changed files with 21 additions and 3 deletions

View File

@ -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'");
}
}

View File

@ -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'");

View File

@ -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;
?>