$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>Delete Custom Posts & Attachments|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Delete Custom Posts & Attachments

matteradmin10PV0评论

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.

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far