Using WordPress ‘post_types_to_delete_with_user’ PHP action

The post_types_to_delete_with_user WordPress PHP filter is used to modify the list of post types that will be deleted when a user is deleted.

Usage

add_filter('post_types_to_delete_with_user', 'my_custom_function', 10, 2);

function my_custom_function($post_types_to_delete, $id) {
  // your custom code here
  return $post_types_to_delete;
}

Parameters

  • $post_types_to_delete (string[]): An array of post types to delete.
  • $id (int): The user ID.

More information

See WordPress Developer Resources: post_types_to_delete_with_user

Examples

Prevent deletion of ‘portfolio’ post type

When a user is deleted, do not delete their ‘portfolio’ post types.

add_filter('post_types_to_delete_with_user', 'prevent_portfolio_deletion', 10, 2);

function prevent_portfolio_deletion($post_types_to_delete, $id) {
  if (($key = array_search('portfolio', $post_types_to_delete)) !== false) {
    unset($post_types_to_delete[$key]);
  }
  return $post_types_to_delete;
}

Delete custom ‘event’ post type

When a user is deleted, delete their ‘event’ post types.

add_filter('post_types_to_delete_with_user', 'delete_event_post_type', 10, 2);

function delete_event_post_type($post_types_to_delete, $id) {
  $post_types_to_delete[] = 'event';
  return $post_types_to_delete;
}

Exclude ‘page’ post type

Prevent the deletion of ‘page’ post types when a user is deleted.

add_filter('post_types_to_delete_with_user', 'exclude_page_post_type', 10, 2);

function exclude_page_post_type($post_types_to_delete, $id) {
  if (($key = array_search('page', $post_types_to_delete)) !== false) {
    unset($post_types_to_delete[$key]);
  }
  return $post_types_to_delete;
}

Allow deletion of multiple custom post types

Delete custom post types ‘event’ and ‘testimonial’ when a user is deleted.

add_filter('post_types_to_delete_with_user', 'delete_multiple_custom_post_types', 10, 2);

function delete_multiple_custom_post_types($post_types_to_delete, $id) {
  array_push($post_types_to_delete, 'event', 'testimonial');
  return $post_types_to_delete;
}

Reset to default post types

Reset the post types to be deleted to the default ‘post’ and ‘attachment’.

add_filter('post_types_to_delete_with_user', 'reset_to_default_post_types', 10, 2);

function reset_to_default_post_types($post_types_to_delete, $id) {
  return array('post', 'attachment');
}

Prevent deletion of custom post type when user is deleted

Prevent the ‘my_custom_post_type’ from being deleted when a user is deleted.

add_filter('post_types_to_delete_with_user', 'prevent_my_custom_post_type_deletion', 10, 2);

function prevent_my_custom_post_type_deletion($post_types_to_delete, $id) {
    $key = array_search('my_custom_post_type', $post_types_to_delete);
    if ($key !== false) {
        unset($post_types_to_delete[$key]);
    }
    return $post_types_to_delete;
}

Add a custom post type to be deleted with user

Add ‘another_custom_post_type’ to the list of post types to delete when a user is deleted.

add_filter('post_types_to_delete_with_user', 'add_another_custom_post_type_to_delete', 10, 2);

function add_another_custom_post_type_to_delete($post_types_to_delete, $id) {
    $post_types_to_delete[] = 'another_custom_post_type';
    return $post_types_to_delete;
}

Delete all post types associated with a specific user role

Delete all post types associated with users that have the ‘editor’ role when they are deleted.

add_filter('post_types_to_delete_with_user', 'delete_post_types_for_editors', 10, 2);

function delete_post_types_for_editors($post_types_to_delete, $id) {
    $user = get_userdata($id);

    if (in_array('editor', $user->roles)) {
        $post_types_to_delete = array_merge($post_types_to_delete, array('custom_post_type1', 'custom_post_type2'));
    }
    return $post_types_to_delete;
}

Delete only posts and pages when a user is deleted

Only delete ‘post’ and ‘page’ post types when a user is deleted.

add_filter('post_types_to_delete_with_user', 'delete_only_posts_and_pages', 10, 2);

function delete_only_posts_and_pages($post_types_to_delete, $id) {
    $post_types_to_delete = array('post', 'page');
    return $post_types_to_delete;
}

Prevent deletion of any post type when a user is deleted

Prevent all post types from being deleted when a user is deleted.

add_filter('post_types_to_delete_with_user', 'prevent_any_post_type_deletion', 10, 2);

function prevent_any_post_type_deletion($post_types_to_delete, $id) {
    return array();
}