here is a code to delete the featured image of a custom post type when moved to Trash. This works. Now I also want to delete a PDF associated to this post. Have a look, I am still doing something wrong... I need some help to understand.

//delete the featured image when a custom post is moved to trash

add_action( 'trashed_post', 'mtp_delete_attached_thumbnail_for_trashed_product', 20, 1 );

function mtp_delete_attached_thumbnail_for_trashed_product( $post_id ) {
 // gets ID of post being trashed
 $post_type = get_post_type( $post_id );
 // only run on custom post type candidate
if ( $post_type != 'candidate' ) {
return true;
 // get ID of featured image
 $post_thumbnail_id = get_post_thumbnail_id( $post_id );

 // delete featured image
 wp_delete_attachment( $post_thumbnail_id, true );
 // !!!works!!!

//  get ID of pdf
//$vita_file = get_field('_cr_dokumente_vita_pdf', $post_id);
// delete vita pdf
//wp_delete_attachment( $vita_file, true );
// !!!not working!!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.