You call wp_delete_attachment
passing $attachmentid
which is undefined, so it can’t work… Also the $force_delete
variable isn’t defined anywhere in your code.
Also there is no point in using normal loop in here – you don’t use template tags in this action, so calling the_post
is just a waste of resources…
add_action( 'set_user_role', function( $user_id, $role, $old_roles )
{
if ( 'basic' == $role ) {
$args = array(
'fields' => 'ids',
'posts_per_page' => -1,
'post_type' => 'attachment',
'post_mime_type' => 'video'
);
$all_ids = new WP_Query( $args );
if ( $all_ids->have_posts() ) {
foreach ( $all_ids->posts as $post ) {
wp_delete_attachment( $post->ID );
// wp_delete_attachment( $post->ID, true ); // <- use this, if you wan't to bypass trash
}
}
}
}, 10, 3 );