WordPress.org

Make WordPress Core

Changeset 5209


Ignore:
Timestamp:
04/07/2007 07:00:19 PM (15 years ago)
Author:
rob1n
Message:

Add wp_add_post_tags() and $append variable for wp_set_post_tags(). fixes #4109

File:
1 edited

Legend:

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

    r5197 r5209  
    785785}
    786786
    787 function wp_set_post_tags( $post_id = 0, $tags = '' ) {
     787function wp_add_post_tags($post_id = 0, $tags = '') {
     788    return wp_set_post_tags($post_id, $tags, true);
     789}
     790
     791function wp_set_post_tags( $post_id = 0, $tags = '', $append = false ) {
     792    /* $append - true = don't delete existing tags, just add on, false = replace the tags with the new tags */
    788793    global $wpdb;
    789794   
     
    792797    if ( !$post_id )
    793798        return false;
    794 
    795     $tags = explode( ',', $tags );
     799   
     800    // prevent warnings for unintialized variables
     801    $tag_ids = array();
     802   
     803    $tags = (is_array($tags)) ? $tags : explode( ',', $tags );
     804   
    796805    foreach ( $tags as $tag ) {
    797806        $tag = trim( $tag );
     
    802811        $tag_ids[] = $tag_id;
    803812    }
    804 
    805     if ( !is_array( $tag_ids ) )
    806         return false;
    807 
     813   
     814    if ( empty($tag_ids) )
     815        return false;
     816   
    808817    $tag_ids = array_unique( $tag_ids );
    809 
     818   
    810819    // First the old tags
    811820    $old_tags = $wpdb->get_col("
     
    813822        FROM $wpdb->post2cat
    814823        WHERE post_id = '$post_id' AND rel_type = 'tag'");
    815 
     824   
    816825    if ( !$old_tags ) {
    817826        $old_tags = array();
     
    819828        $old_tags = array_unique( $old_tags );
    820829    }
    821 
     830   
    822831    // Delete any?
    823832    $delete_tags = array_diff( $old_tags, $tag_ids);
    824     if ( $delete_tags ) {
     833    if ( $delete_tags && !$append ) {
    825834        foreach ( $delete_tags as $del ) {
    826835            $wpdb->query("
     
    832841        }
    833842    }
    834 
     843   
    835844    // Add any?
    836845    $add_tags = array_diff( $tag_ids, $old_tags );
     
    844853        }
    845854    }
    846 
     855   
    847856    // Update category counts.
    848857    $all_affected_tags = array_unique( array_merge( $tag_ids, $old_tags ) );
Note: See TracChangeset for help on using the changeset viewer.