1

I would like to delete some of the posts in WordPress between a period based on the author via MYSQL database. I have difficulty identifying the WordPress tables and their relationships to perform the following delete operation. The below query is just an example(table and column names could be wrong). Appreciate your help.

Delete from wp-posts where post-author = 'name' AND published-date BETWEEN 01/01/2024 AND 20/03/2024;

1 Answer 1

0

This is what I wanted to achieve, I hope it helps somebody

DELETE wp_posts FROM wp_posts 
INNER JOIN wp_users ON wp_posts.post_author = wp_users.ID 
WHERE post_author = 1 AND post_date BETWEEN '2024-01-01 00:00:00' AND '2024-03-28 23:59:59';

3
  • Note that this will delete the posts from the database, but if—as your question's title suggests—you also wanted to remove the media files uploaded by that user, this won't do it. The media files are stored in the filesystem. This will remove the information WordPress "knows" about those files, like title, alt tag, etc, but won't remove the files themselves.
    – Pat J
    Commented Mar 28 at 14:49
  • @PatJ Yes, I could see these pictures in the file manager even though these pictures disappeared from the media library of the WordPress dashboard. These pictures in the file manager should be removed manually? or is there a way in the database itself Commented Mar 28 at 15:24
  • The DB can't affect the filesystem. Unfortunately, once you've removed the posts via SQL (which includes the attachment post type, which is how WP stores info about the media files), there's no way to know who uploaded a given media file. The better way would be to use WordPress functions like wp_delete_post() and wp_delete_attachment() instead of SQL queries, because those will make sure that all the necessary things are handled.
    – Pat J
    Commented Mar 28 at 16:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.