I need to write a SQL script to delete users with no posts associated with them in a WordPress database. I tried this script after doing some searching:
DELETE FROM wp_users WHERE ID NOT IN (SELECT DISTINCT post_author FROM wp_105_posts)
but this deleted all the users. Can someone let me know what I am doing wrong please?
Seems like either none of the users made posts, or
wp_105_posts.post_authordoesn’t link towp_users.ID. Are you surewp_105_posts.post_authoris a Foreign Key ofwp_users.ID?wp_105_postsshould have one column that references who the user is in thewp_userstable. Since your GUID Primary Key forwp_usersisID, this should be the one column you use to reference the user from your post tables. Here is a small example:user_table
ID | name | age | email | etc.
1 | nick | 24 | nick@com.com | ..
2 | bob | 30 | bob@com.com | …
3 | sue | 35 | sue@com.com | …
a_post_table
ID (ID of post) | User_ID (ID of user) | title | date | body
……..1…………..|…1 (post by nick)…….. | help | 1/1/11
……..2…………..|…1 (post by nick)…….. | help | 3/2/11
……..3…………..|…2 (post by bob)…….. | help | 5/6/11
As you can see, the post table knows everything about the author by simply holding its ID. You can use a join query to get all user info from the post table just by knowing its ID. Using this query now
Will delete Sue, and Sue only, given the data above.
HTH