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
- Find out if logged in user is not subscriber
- Get multiple roles with get_users
- User-edit role setting distinct from wp_capabilities? [closed]
- Delete all subscribers from wp_users and wp_usermeta a few thousand at a time
- WordPress and event-driven programming – what is it about?
- Allowing an email as the username?
- Get the name of user who updated post
- Change user’s display name programmatically
- Send activation email to user after signup [duplicate]
- wp_update_user() does not update user_data
- Admin Post Update Redirection to Posts Screen
- Set user after wp_create_user?
- How to add follow functionality to multi-author wordpress site?
- Search multiple meta keys at once
- Redirecting user after updating profile?
- How to add new custom field in default add user form through plugin
- ACF Upload Image in front-end with custom form
- How to place HTML below the title of the (custom) post overview
- Can I bulk register contributors for a new blog
- show cimy user fields in users.php with manage_users_columns
- How to add multiple checkbox elements to media attachments?
- Why i getting blank user_activation_key in get_user_by( ‘login’, $login ) function?
- how do i get a greeting for logged in uses by the time of day
- How can i create an array user meta?
- How can I check if a user’s email exists in the database
- Getting users by specific capability, not role
- Creating user relationships
- wordpress user roles are not working
- Execute a ultimate member action when user role is updated
- What is proper way to store info such as user marking a post as favorite? In post meta or user meta?
- What are some best practices for user exit strategy?
- Is there a way to identify a user in a custom REST API method? [duplicate]
- WP_User_Query – searching multiple fields
- wordpress disable login for unverified user
- Problem with automatic role change through cron job
- Profile page for user roles
- User can not login
- Search Function on a plugin WordPress User
- Print profile details as PDF or from modal window
- No more administrator roles / permissions after WordPress upgrade
- How to display text if profile fields are not filled?
- Order users by user role
- Get user by meta key – WP multi site
- Force users to use password with specifications
- What hooks do I need to hook into to capture ever wp_user creation/change?
- Give users alternative/second avatar that is rectangle sized
- Inserting current user ID into Post for Author
- Difference between is_user_logged_in and $_session[‘uname’]
- loginout function customization
- Assigning multiple or additional capabilities to specific users or how to create additional roles like bbpress roles?
- Using get_user in wordpress with sorting
- Using my own user table
- How can I add authors in WordPress and assign them a picture?
- wp_generate_password sets password but can’t login using created password
- Transferring theme and imported post attachments
- Personalized Gallery what are my options?
- How to verify which WordPress user requested the API in ASP .NET Core?
- How to make WordPress search page use a relative path
- Get users meta and show to attribute
- Custom Password Reset
- Can I add a unique code for each user based on the location?
- Is nicename the same as nickname in WP?
- 502 Error when edit some users
- How to update a user with REST API v2 knowing only the username?
- How do I delete a wordpress user from giant database?
- How to prevent users from deleting their accounts?
- Display recent members
- Update user counts in admin interface
- To save user info on the same page by form submiting
- Groups roles & capabilities
- connect users to Custom post types
- Get “Image CSS Class” value from the Advanced Options section
- Button for users to upgrade their user role + Button to show current user role!
- How do I Limit the number of pages a non-subscriber can see?
- Username has been exposed
- How To Find The ID Of All Registered Users?
- Restrict access to certain dashboard pages based on user id
- how to show logged in members username in wordpress content
- How to expire guest users after 1.5 hours logged in?