post_types_to_delete_with_user
Allows you to change the list of post types to delete when a user is deleted. This applies only to posts created by the user.
By default, the deletion list includes post types whose delete_with_user parameter was enabled during registration. See register_post_type().
Usage
add_filter( 'post_types_to_delete_with_user', 'wp_kama_post_types_to_delete_with_user_filter', 10, 2 );
/**
* Function for `post_types_to_delete_with_user` filter-hook.
*
* @param string[] $post_types_to_delete Array of post types to delete.
* @param int $id User ID.
*
* @return string[]
*/
function wp_kama_post_types_to_delete_with_user_filter( $post_types_to_delete, $id ){
// filter...
return $post_types_to_delete;
}
- $post_types_to_delete(string[])
- An array of post type names.
- $id(integer)
- The ID of the user being deleted.
Examples
#1 Add the product post type to deletion when a user is deleted
Suppose the registered product post type has delete_with_user set to false, meaning the user's posts should not be deleted when the user is deleted. Change this behavior through the hook so the user's product posts are deleted together with the user.
add_filter( 'post_types_to_delete_with_user', 'add_post_type_product_to_delete_with_user', 10, 2 );
function add_post_type_product_to_delete_with_user( $post_types_to_delete, $id ){
$post_types_to_delete[] = 'product';
return $post_types_to_delete;
}
The same result can be achieved with the register_post_type_args hook:
add_filter( 'register_post_type_args', 'register_post_type_product_args', 10, 2 );
function change_rest_base_posts( $args, $post_type ) {
if ( 'product' === $post_type ) {
$args['delete_with_user'] = true;
}
return $args;
}Changelog
| Since 3.4.0 | Introduced. |
Where the hook is called
wp-admin/includes/user.php 406
$post_types_to_delete = apply_filters( 'post_types_to_delete_with_user', $post_types_to_delete, $id );