wordpress删除文章时自动删除文章图片

建站教程 心晴客栈 240次浏览 0个评论 扫描二维码
/* 删除文章时删除图片附件 */ 
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'); 
/* 删除文章时删除图片附件over */
支持博主

心晴客栈 , 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明wordpress删除文章时自动删除文章图片
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址