我有一个自定义的帖子类型,叫做“新来者”。我正在尝试整合一个功能,当CPT帖子更新或发布时,该功能将删除自定义帖子类型的所有图片附件(除了特色图片)。但这似乎不起作用,我做错了什么?
function delete_extra_images() {
if ( $post->post_type == \'newcomer\' && $post->post_status == \'publish\' ) {
$attachments = get_posts( array(
\'post_type\' => \'attachment\',
\'posts_per_page\' => -1,
\'post_parent\' => $post->ID,
\'exclude\' => get_post_thumbnail_id()
));
if ( $attachments ) {
foreach ( $attachments as $attachment ) {
wp_delete_attachment( $attachment->ID, false );
}
}
}
}
add_action( \'publish_newcomer\', \'delete_extra_images\' );