admin管理员组文章数量:1130349
I'm trying to setup a script that will automatically delete all posts of a custom post type, including their attachments. The first part of the script seems to work fine:
$allposts= get_posts( array('post_type'=>'wpbb_application','numberposts'=>-1) );
foreach ($allposts as $eachpost) {
wp_delete_post( $eachpost->ID, true );
}
This checks for all posts of the 'wpbb_application' CPT, and then deletes them. It works fine as far as I can tell, but it leaves the PDFs that are attached to the posts in the media library. What I would like to do is automatically remove these attachments too. I therefore added this to precede the code:
add_action( 'before_delete_post', function( $id ) {
$attachments = get_attached_media( 'application', $id );
foreach ($attachments as $attachment) {
wp_delete_attachment( $attachment->ID, 'true' );
}
} );
I can't seem to get that to work for some reason. Can anyone shed any light on this?
Thanks.
I'm trying to setup a script that will automatically delete all posts of a custom post type, including their attachments. The first part of the script seems to work fine:
$allposts= get_posts( array('post_type'=>'wpbb_application','numberposts'=>-1) );
foreach ($allposts as $eachpost) {
wp_delete_post( $eachpost->ID, true );
}
This checks for all posts of the 'wpbb_application' CPT, and then deletes them. It works fine as far as I can tell, but it leaves the PDFs that are attached to the posts in the media library. What I would like to do is automatically remove these attachments too. I therefore added this to precede the code:
add_action( 'before_delete_post', function( $id ) {
$attachments = get_attached_media( 'application', $id );
foreach ($attachments as $attachment) {
wp_delete_attachment( $attachment->ID, 'true' );
}
} );
I can't seem to get that to work for some reason. Can anyone shed any light on this?
Thanks.
本文标签: Delete Custom Posts amp Attachments
版权声明:本文标题:Delete Custom Posts & Attachments 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749038259a2306724.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论