The hook you choose is appropriate, and here is how to use it to delete all posts of all types (posts, pages, links, attachments, etc) of the deleted user:
add_action('delete_user', 'my_delete_user');
function my_delete_user($user_id) {
$args = array (
'numberposts' => -1,
'post_type' => 'any',
'author' => $user_id
);
// get all posts by this user: posts, pages, attachments, etc..
$user_posts = get_posts($args);
if (empty($user_posts)) return;
// delete all the user posts
foreach ($user_posts as $user_post) {
wp_delete_post($user_post->ID, true);
}
}
If you only want to delete user attachments, change the post_type
arguments from any
to attachment
and use wp_delete_attachment($attachment_id)
instead of wp_delete_post()
.
Related Posts:
- New User Save Filter
- How to restrict access to uploaded files?
- Automatically delete inactive users after 2 months
- WordPress auto login after registration not working
- Execute a function when admin changes the user role
- Disable delete user
- Force display name as full name
- How to discover and delete unused accounts?
- Extending WP_User class and using this sub-class during the whole lifecycle
- Allow guests to save favourite pages?
- How do I properly format the user_role array?
- User fields that can be edited by administrator?
- Does wp_delete_user() remove all user content?
- Prevent attachment from being deleted
- Bulk delete users from a csv list
- user_profile_update_errors hook not executing
- Restrict access to non-wordpress section of site with user roles?
- Delete a user from frontend
- Privilege to recover trashed posts
- New User ID not working in custom function with user_register hook
- Retrieve New user’s ID
- If the current user is an administrator or editor
- How to change the default registration email ? (plugin and/or non-plugin)
- Editor can create any new user except administrator
- How do I add a field on the Users profile? For example, country, age etc
- How To Add Custom Form Fields To The User Profile Page?
- How can I edit post data before it is saved?
- How do you use a CPT as the default home page?
- How do I display logged-in username IF logged-in?
- How to allow an user role to create a new user under a role which lower than his level only?
- user_login vs. user_nicename
- How to programatically change username (user_login)?
- Creating an Image-Centric Custom Post Type?
- Change the Author Slug from Username to Nickname
- Remove Ability for Other Users to View Administrator in User List?
- Difference between update_user_meta and update_user_option
- Is there a way to set default custom fields when creating a post?
- Make display name unique
- Make WooCommerce pages accessible for logged in users only
- Find out if logged in user is not subscriber
- WordPress usermeta scaling for thousands of users
- Use wp init hook to call other hooks?
- How to get WordPress Username in Array format
- Display user registration date
- Get multiple roles with get_users
- get_user_meta() doesn’t include user email?
- Correctly delete posts with meta and attachments [duplicate]
- Confirmation required on email change
- How to Merge Two Authors Into One?
- Whats the best way to share user data across multiple WordPress websites?
- get_current_user_id() returns 0?
- How to get userid at wp_logout action hook?
- Groups of capabilities: users with multiple roles?
- Is there a way to merge two users?
- User-edit role setting distinct from wp_capabilities? [closed]
- List users by last name in WP_User_Query
- What’s the difference between the capability remove_users and delete_users?
- How to change user_login with wp-cli?
- How to add a new product type on woocommerce product types? [closed]
- Delete all subscribers from wp_users and wp_usermeta a few thousand at a time
- Email user when password is reset by admin
- Replacing the WordPress password validation
- Ban a user and end their session
- WordPress and event-driven programming – what is it about?
- Allowing users to edit only their page and nobody else’s
- How can 2 blogs share the same users
- alphabetically order role drop-down selection in dashboard
- WordPress hook before inserting post into database
- Making Custom Fields Standard in the Admin UI
- Find out who deleted a page or post?
- Change the author slug from nickname to ID
- How to let contributors to create a new revision(draft) editing their published posts
- how to use joomla password format in wordpress?
- How to do get_users() with multiple meta_keys
- Disallowing Users of a Custom Role from Deleting or Adding Administrators?
- What the user_status column?
- On which hook should I be calling register_nav_menu(s)?
- How to limit users to one comment per post
- Different back-end language for different users?
- Hide Admin Menu for Specific User ID who has administrator Role
- Migrating WordPress users into Disqus
- Problem with Hebrew characters in username
- Is there an upper limit for users in WP?
- How to display the status of users (online – offline) in archive.php
- Remove email verification when new user register
- How to change user`s avatar?
- How to set up User email verification after Signup?
- Allow up to 5 Concurrent Login Sessions
- How to let user set password on registration
- Using filters and actions for plugin API?
- How to hide media uploads by other users in the Media menu?
- Show admin bar only for some USERS roles
- Add custom html to last sub-menu item
- How to display custom user meta from registration in backend?
- Allowing an email as the username?
- How to update the delete user confirmation form?
- How to work around “that email address has already been used” error?
- Get the name of user who updated post
- Issue with get_theme_mod returning a blank value instead of the saved value
- Grouping users under parent user