WordPress (Codex): Update tags with Post ID


There is a no wp_update_tags in WordPress codex.But we realize it through an indirect method.Accordingly, the function below; first, remove the tags related with post_ID and insert new tags.

        // Coded by tankado at tankado dot com
	function wp_update_post_tags($post_ID, $tags)
		global $wpdb;
		// Delete related tags from database
		$sql = "DELETE FROM wp_term_relationships ";
		$sql .= "WHERE wp_term_relationships.object_id = '$post_ID' ";
		$sql .= "AND wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id ";
		$sql .= "AND wp_term_taxonomy.taxonomy = 'post_tag'";
		$wpdb->query( $sql );

		// Insert new tags to database
		wp_set_post_tags($post_ID, $tags, false );


This site uses Akismet to reduce spam. Learn how your comment data is processed.