Note
使用风险自负,这是一个错误,我遇到过几个实例,它会删除所有附件。不确定原因删除页面时,是否可以删除与该页面关联的媒体?我知道在“插入媒体”页面中,您可以通过“上载到此页面”的图像进行过滤,因此我可以获得这些图像的列表,并在删除页面时删除它们吗?
现在我正在玩弄挂接删除帖子。马上这没什么作用,但我想我已经有所进展了。
function del_post_media($pid) {
$query = "DELETE FROM wp_postmeta
WHERE ".$pid." IN
(
SELECT id
FROM wp_posts
WHERE post_type = \'attachment\'
)";
global $wpdb;
if ($wpdb->get_var($wpdb->prepare($query))) {
return $wpdb->query($wpdb->prepare($query));
}
return true;
}
add_action(\'delete_post\', \'del_post_media\');
最合适的回答,由SO网友:Charles Clarkson 整理而成
这个怎么样?它采用了get_posts() 功能参考页。
function delete_post_media( $post_id ) {
$attachments = get_posts( array(
\'post_type\' => \'attachment\',
\'posts_per_page\' => -1,
\'post_status\' => \'any\',
\'post_parent\' => $post_id
) );
foreach ( $attachments as $attachment ) {
if ( false === wp_delete_attachment( $attachment->ID ) ) {
// Log failure to delete attachment.
}
}
}
add_action( \'before_delete_post\', \'delete_post_media\' );