有時我們寫好一篇文章又覺得不合適發出來 , 都會選擇刪除~

雖然文章已經刪除 , 但是圖片及附件卻原整保存在upload文件夾中...

所以想徹底刪除文章 + 圖片 + 附件 , 我們需要動點手腳...新增代碼~

做法很簡單 , 只需要把以下代碼放在functions.php文件中就完成了~


贊助商連結

代碼 :

/* 删除文章时删除图片附件/* ------------------------ */function delete_post_and_attachments($post_ID) {global $wpdb;//删除特色图片$thumbnails = $wpdb->get_results( "SELECT * FROM $wpdb->postmeta WHERE meta_key = '_thumbnail_id' AND post_id = $post_ID" );foreach ( $thumbnails as $thumbnail ) {wp_delete_attachment( $thumbnail->meta_value, true );}//删除图片附件$attachments = $wpdb->get_results( "SELECT * FROM $wpdb->posts WHERE post_parent = $post_ID AND post_type = 'attachment'" );foreach ( $attachments as $attachment ) {wp_delete_attachment( $attachment->ID, true );}$wpdb->query( "DELETE FROM $wpdb->postmeta WHERE meta_key = '_thumbnail_id' AND post_id = $post_ID" );}add_action('before_delete_post', 'delete_post_and_attachments');

*友情提醒 , 刪除是即時性 , 請想好再行操作刪除程序~