Php 仅在自定义post类型数组上运行函数

Php 仅在自定义post类型数组上运行函数,php,wordpress,function,hook,custom-post-type,Php,Wordpress,Function,Hook,Custom Post Type,我只想在给定的自定义帖子类型上运行以下代码。现在它只在一个特定的自定义类型“file”上运行 我试图在数组中添加函数,但我确信这不是正确的 //用于删除帖子时删除附件 添加操作('before_delete_post'、'mtp_delete_attached_缩略图_for_trashed_product'、20,1); 功能mtp\u删除\u附加的\u缩略图\u用于垃圾产品($post\u id){ //获取被丢弃的帖子的ID $post\u type=get\u post\u type(

我只想在给定的自定义帖子类型上运行以下代码。现在它只在一个特定的自定义类型“file”上运行

我试图在数组中添加函数,但我确信这不是正确的

//用于删除帖子时删除附件
添加操作('before_delete_post'、'mtp_delete_attached_缩略图_for_trashed_product'、20,1);
功能mtp\u删除\u附加的\u缩略图\u用于垃圾产品($post\u id){
//获取被丢弃的帖子的ID
$post\u type=get\u post\u type($post\u id);
//不在其他帖子类型上运行
如果($post_type!=“file”){
返回true;
}
//获取特征图像的ID
$post\u thumboil\u id=get\u post\u thumboil\u id($post\u id);
//删除特色图片
wp_delete_附件($post_缩略图_id,true);
}
您可以使用in_数组()来简化此操作

// For deleting attachments when Deleting POSTS
add_action( 'before_delete_post', 'mtp_delete_attached_thumbnail_for_trashed_product', 20, 1 );

function mtp_delete_attached_thumbnail_for_trashed_product( $post_id ) {

 // List of post types.
  $post_types = array(
          'file',
          'share',
          'folder',
   );

 // gets ID of post being trashed
 $post_type = get_post_type( $post_id );


 // does not run on other post types
 if ( ! in_array( $post_type, $post_types, true) ) {
 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 );

}

由于某些原因,它不适用于任何职位类型。我试着只保留一种帖子类型,但也不起作用。@Nash当帖子类型为文件、共享或文件夹时,您想删除特色图片,是吗?是的,完全正确。从帖子类型文件、共享或文件夹中删除帖子时,请查找特征图像并将其删除。@Nash太好了。很乐意帮忙。