Selectively restoring original posts from a compromised site to a freshly installed WordPress database

Take a copy of your data file and store it safely just in case you mess up – it’s easily done. Make sure it is labelled as hacked so you can’t accidentally use it anywhere.

Look in your SQL for a section starting with:

INSERT INTO `wp_posts` VALUES

and eventually ending with a semicolon.

This is all of your post data. Hopefully you have one line for each post otherwise this will be very hard. This does not include any categories, tags or custom fields.

You will hopefully see lines starting something like this:

INSERT INTO `wp_posts` VALUES (1,1,'2015-03-03 11:42:37','20
(2,1,'2015-03-03 11:42:37','2015-03-03 11:42:37','what is th
(3,1,'2015-03-03 11:51:21','0000-00-00 00:00:00','','Auto Dr
(5,1,'2015-03-03 11:54:41','2015-03-03 11:54:41','','Berry a
(6,1,'2015-03-03 11:55:16','2015-03-03 11:55:16','what is th
(7,1,'2015-03-03 11:55:21','0000-00-00 00:00:00','','Auto Dr
(8,1,'2015-03-03 12:00:01','2015-03-03 12:00:01','We are a n
(9,1,'2015-03-03 12:00:01','2015-03-03 12:00:01','We are a n
(10,1,'2015-03-03 12:01:30','2015-03-03 12:01:30','Due to th
(11,1,'2015-03-03 12:01:30','2015-03-03 12:01:30','Due to th
(12,1,'2015-03-03 12:01:43','2015-03-03 12:01:43','Applicati
(13,1,'2015-03-03 12:01:43','2015-03-03 12:01:43','Applicati
(14,1,'2015-03-03 12:02:06','2015-03-03 12:02:06','Participa
(15,1,'2015-03-03 12:02:06','2015-03-03 12:02:06','Participa
(16,1,'2015-03-03 12:03:16','2015-03-03 12:03:16','Walk for 
(17,1,'2015-03-03 12:03:13','2015-03-03 12:03:13','','461569

You “simply” (and this can be slow, careful work) want to delete the lines which have hacked data in. Any line that doesn’t look like your original post can go.

Examining one of those lines in detail and formatting it to see what’s what:

INSERT INTO `wp_posts` VALUES (
1, -- this is the Post ID
1, -- this is the author ID
'2015-03-03 11:42:37', -- post date
'2015-03-03 11:42:37', -- post date in GMT

and then:

'Welcome to WordPress. This is your first post. Edit or delete it, then start blogging!',

That field is your post content. If that looks like you don’t want it in your database then delete the whole line from ( up to ),

'Hello world!', -- Post title
'', -- Excerpt
'publish', -- status - all live posts will have "publish" here

-- and so on
'open','open','','hello-world','','',
'2015-03-03 11:42:37','2015-03-03 11:42:37',
'',0,'http://testsite.localhost/?p=1',0,'post','',1),

If any fields have something in that isn’t plain English (if English your posts are) or clearly a simple value, date or URL related to your site then get rid of the line.

While doing this manually can be done, if there are many posts I would really consider getting someone who knows their way around WP & MySQL to clean the data for you. (Not me at the moment, I should add!!)

Backup your clean site before trying to import records just in case.

Good luck!