Delete all the custom post types when deleting user

delete all custom post type when delete user

I have a problem in custom post types, when I remove user, I can’t deleted there post or attribute all posts.

Solutions Collecting From Web of "Delete all the custom post types when deleting user"

If you will delete the custom posts, if you kill a user, then hook inside the delete_user Hook. This hook give you the possibility to call actions and doing other jobs, like delete posts to this user id.

See the codex for more information and a example source.

I use custom posts on my site and once the user is deleted all their custom posts also go.

Are you sure that in your code that the user ID is the correct author for those posts – depending on how it is coded the author can end up being the administrator. I have seen before with custom post moderation that the author is actually stored as a separate meta tag but wordpress thinks the author is actually the admin that approved it.

It depends on how your theme has been written and how other functions on your website work.

I would recommend having a look at one of these custom posts to make sure who the registered author is, if it is different you will need to put some sort of post query in the delete_user hook as suggested by @bueltge and delete the found posts with author meta tag == user

1) Your post types that you want to delete, they must have an active field for the author (register_post_type -> supports -> author).

2) Or you can use the post_types_to_delete_with_user filter, see:

add_filter("post_types_to_delete_with_user", "post_types_to_delete_with_user_callback", 10, 2);

function post_types_to_delete_with_user_callback(array $postTypesToDelete, $userId) {
  $postTypesToDelete[] = "YOUR_POST_TYPE_KEY";
  return $postTypesToDelete;