Make WordPress Core


Ignore:
Timestamp:
05/23/2007 06:07:53 PM (19 years ago)
Author:
ryan
Message:

Set and get post cats to taxonomy. see #4189

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/post.php

    r5515 r5529  
    448448    $post_id = (int) $post_id;
    449449
    450     $cats = &get_the_category($post_id);
    451     $cat_ids = array();
    452     foreach ( $cats as $cat )
    453         $cat_ids[] = (int) $cat->cat_ID;
    454     return array_unique($cat_ids);
     450    $cats = get_object_terms($post_id, 'category', 'get=ids');
     451    return $cats;
    455452}
    456453
     
    806803        $post_categories = array(get_option('default_category'));
    807804
     805    $post_categories = array_map('intval', $post_categories);
    808806    $post_categories = array_unique($post_categories);
    809807
    810     // First the old categories
    811     $old_categories = $wpdb->get_col("
    812         SELECT category_id
    813         FROM $wpdb->post2cat
    814         WHERE post_id = '$post_ID' AND rel_type = 'category'");
    815 
    816     if (!$old_categories) {
    817         $old_categories = array();
    818     } else {
    819         $old_categories = array_unique($old_categories);
    820     }
    821 
    822     // Delete any?
    823     $delete_cats = array_diff($old_categories,$post_categories);
    824 
    825     if ($delete_cats) {
    826         foreach ($delete_cats as $del) {
    827             $wpdb->query("
    828                 DELETE FROM $wpdb->post2cat
    829                 WHERE category_id = '$del'
    830                     AND post_id = '$post_ID' AND rel_type = 'category'
    831                 ");
    832         }
    833     }
    834 
    835     // Add any?
    836     $add_cats = array_diff($post_categories, $old_categories);
    837 
    838     if ($add_cats) {
    839         foreach ($add_cats as $new_cat) {
    840             $new_cat = (int) $new_cat;
    841             if ( !empty($new_cat) )
    842                 $wpdb->query("
    843                     INSERT INTO $wpdb->post2cat (post_id, category_id)
    844                     VALUES ('$post_ID', '$new_cat')");
    845         }
    846     }
    847 
    848     // Update category counts.
    849     $all_affected_cats = array_unique(array_merge($post_categories, $old_categories));
    850     foreach ( $all_affected_cats as $cat_id ) {
    851         $count = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->post2cat, $wpdb->posts WHERE $wpdb->posts.ID=$wpdb->post2cat.post_id AND post_status = 'publish' AND post_type = 'post' AND category_id = '$cat_id' AND rel_type = 'category'");
    852         $wpdb->query("UPDATE $wpdb->categories SET category_count = '$count', type = type | " . TAXONOMY_CATEGORY . " WHERE cat_ID = '$cat_id'");
    853         clean_category_cache($cat_id);
    854         do_action('edit_category', $cat_id);
    855     }
     808    return wp_set_object_terms($post_ID, $post_categories, 'category');
    856809}   // wp_set_post_categories()
    857810
Note: See TracChangeset for help on using the changeset viewer.