Definition:
function wp_delete_term( $term, $taxonomy, $args = array() {}
Parameters
- int $term: Term ID
- string $taxonomy: Taxonomy Name
- array|string $args: Optional. Change ‘default’ term id and override found term ids.
Return values
returns:Returns false if not term; true if completes delete action.
Defined actions
- edit_term_taxonomies
do_action( 'edit_term_taxonomies', $edit_tt_ids ); - edited_term_taxonomies
do_action( 'edited_term_taxonomies', $edit_tt_ids ); - delete_term_taxonomy
do_action( 'delete_term_taxonomy', $tt_id ); - deleted_term_taxonomy
do_action( 'deleted_term_taxonomy', $tt_id ); - delete_term
do_action('delete_term', $term, $tt_id, $taxonomy); - delete_$taxonomy
do_action("delete_$taxonomy", $term, $tt_id);
Source code
function wp_delete_term( $term, $taxonomy, $args = array() ) {
global $wpdb;
$term = (int) $term;
if ( ! $ids = term_exists($term, $taxonomy) )
return false;
if ( is_wp_error( $ids ) )
return $ids;
$tt_id = $ids['term_taxonomy_id'];
$defaults = array();
if ( 'category' == $taxonomy ) {
$defaults['default'] = get_option( 'default_category' );
if ( $defaults['default'] == $term )
return 0; // Don't delete the default category
}
$args = wp_parse_args($args, $defaults);
extract($args, EXTR_SKIP);
if ( isset( $default ) ) {
$default = (int) $default;
if ( ! term_exists($default, $taxonomy) )
unset($default);
}
// Update children to point to new parent
if ( is_taxonomy_hierarchical($taxonomy) ) {
$term_obj = get_term($term, $taxonomy);
if ( is_wp_error( $term_obj ) )
return $term_obj;
$parent = $term_obj->parent;
$edit_tt_ids = $wpdb->get_col( "SELECT `term_taxonomy_id` FROM $wpdb->term_taxonomy WHERE `parent` = " . (int)$term_obj->term_id );
do_action( 'edit_term_taxonomies', $edit_tt_ids );
$wpdb->update( $wpdb->term_taxonomy, compact( 'parent' ), array( 'parent' => $term_obj->term_id) + compact( 'taxonomy' ) );
do_action( 'edited_term_taxonomies', $edit_tt_ids );
}
$objects = $wpdb->get_col( $wpdb->prepare( "SELECT object_id FROM $wpdb->term_relationships WHERE term_taxonomy_id = %d", $tt_id ) );
foreach ( (array) $objects as $object ) {
$terms = wp_get_object_terms($object, $taxonomy, array('fields' => 'ids', 'orderby' => 'none'));
if ( 1 == count($terms) && isset($default) ) {
$terms = array($default);
} else {
$terms = array_diff($terms, array($term));
if (isset($default) && isset($force_default) && $force_default)
$terms = array_merge($terms, array($default));
}
$terms = array_map('intval', $terms);
wp_set_object_terms($object, $terms, $taxonomy);
}
// Clean the relationship caches for all object types using this term
$tax_object = get_taxonomy( $taxonomy );
foreach ( $tax_object->object_type as $object_type )
clean_object_term_cache( $objects, $object_type );
do_action( 'delete_term_taxonomy', $tt_id );
$wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->term_taxonomy WHERE term_taxonomy_id = %d", $tt_id ) );
do_action( 'deleted_term_taxonomy', $tt_id );
// Delete the term if no taxonomies use it.
if ( !$wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->term_taxonomy WHERE term_id = %d", $term) ) )
$wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->terms WHERE term_id = %d", $term) );
clean_term_cache($term, $taxonomy);
do_action('delete_term', $term, $tt_id, $taxonomy);
do_action("delete_$taxonomy", $term, $tt_id);
return true;
}
3599

February 12, 2011 


wp_delete_post
Definition:
function wp_delete_post( $postid = 0, $force_delete = false ) {}
Parameters
Return values
returns:False on failure
Defined actions
do_action('before_delete_post', $postid);do_action( 'delete_comment', $comment_ids );do_action( 'deleted_comment', $comment_ids );do_action( 'delete_postmeta', $post_meta_ids );do_action( 'deleted_postmeta', $post_meta_ids );do_action( 'delete_post', $postid );do_action( 'deleted_post', $postid );do_action('after_delete_post', $postid);Source code
function wp_delete_post( $postid = 0, $force_delete = false ) { global $wpdb, $wp_rewrite; if ( !$post = $wpdb->get_row($wpdb->prepare("SELECT * FROM $wpdb->posts WHERE ID = %d", $postid)) ) return $post; if ( !$force_delete && ( $post->post_type == 'post' || $post->post_type == 'page') && get_post_status( $postid ) != 'trash' && EMPTY_TRASH_DAYS ) return wp_trash_post($postid); if ( $post->post_type == 'attachment' ) return wp_delete_attachment( $postid, $force_delete ); do_action('before_delete_post', $postid); delete_post_meta($postid,'_wp_trash_meta_status'); delete_post_meta($postid,'_wp_trash_meta_time'); wp_delete_object_term_relationships($postid, get_object_taxonomies($post->post_type)); $parent_data = array( 'post_parent' => $post->post_parent ); $parent_where = array( 'post_parent' => $postid ); if ( 'page' == $post->post_type) { // if the page is defined in option page_on_front or post_for_posts, // adjust the corresponding options if ( get_option('page_on_front') == $postid ) { update_option('show_on_front', 'posts'); delete_option('page_on_front'); } if ( get_option('page_for_posts') == $postid ) { delete_option('page_for_posts'); } // Point children of this page to its parent, also clean the cache of affected children $children_query = $wpdb->prepare("SELECT * FROM $wpdb->posts WHERE post_parent = %d AND post_type='page'", $postid); $children = $wpdb->get_results($children_query); $wpdb->update( $wpdb->posts, $parent_data, $parent_where + array( 'post_type' => 'page' ) ); } else { unstick_post($postid); } // Do raw query. wp_get_post_revisions() is filtered $revision_ids = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_parent = %d AND post_type = 'revision'", $postid ) ); // Use wp_delete_post (via wp_delete_post_revision) again. Ensures any meta/misplaced data gets cleaned up. foreach ( $revision_ids as $revision_id ) wp_delete_post_revision( $revision_id ); // Point all attachments to this post up one level $wpdb->update( $wpdb->posts, $parent_data, $parent_where + array( 'post_type' => 'attachment' ) ); $comment_ids = $wpdb->get_col( $wpdb->prepare( "SELECT comment_ID FROM $wpdb->comments WHERE comment_post_ID = %d", $postid )); if ( ! empty($comment_ids) ) { do_action( 'delete_comment', $comment_ids ); foreach ( $comment_ids as $comment_id ) wp_delete_comment( $comment_id, true ); do_action( 'deleted_comment', $comment_ids ); } $post_meta_ids = $wpdb->get_col( $wpdb->prepare( "SELECT meta_id FROM $wpdb->postmeta WHERE post_id = %d ", $postid )); if ( !empty($post_meta_ids) ) { do_action( 'delete_postmeta', $post_meta_ids ); $in_post_meta_ids = "'" . implode("', '", $post_meta_ids) . "'"; $wpdb->query( "DELETE FROM $wpdb->postmeta WHERE meta_id IN($in_post_meta_ids)" ); do_action( 'deleted_postmeta', $post_meta_ids ); } do_action( 'delete_post', $postid ); $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->posts WHERE ID = %d", $postid )); do_action( 'deleted_post', $postid ); if ( 'page' == $post->post_type ) { clean_page_cache($postid); foreach ( (array) $children as $child ) clean_page_cache($child->ID); $wp_rewrite->flush_rules(false); } else { clean_post_cache($postid); } wp_clear_scheduled_hook('publish_future_post', array( $postid ) ); do_action('after_delete_post', $postid); return $post; }3595