comments
property is there.Following is the JavaScript code used to extract only the comments and get them in the right format to be exported in WordPress database, i.e. change their properties to have the column names in the wp_comments
table.
FYI: You can import JSON data in a MySQL table. That’s what we’re gonna do.
You’ll need node
to run this code and it’ll create a file called comments.json
with the results
1const data = require('./data')
2const fs = require('fs')
3const EventEmitter = require('events').EventEmitter
4const fileEvents = new EventEmitter()
5
6// console.info('All data', data)
7
8let mappedComments = []
9
10data.map(object => {
11 // only get objects where there are comments
12 if (object['og_object'].hasOwnProperty('comments')) {
13 let comments = object['og_object']['comments']['data'] // array of objects
14
15 comments.map(item => {
16 let commentEntry = {
17 comment_author_email: 'info@mydomain.com', // required
18 comment_author: item.from.name, // reuired
19 comment_author_url: 'https://facebook.com/' + item.from.id, // create the URL so that it's the Facebook user's profile link
20 comment_content: item.message,
21 comment_date: item.created_time,
22 comment_date_gmt: item.created_time,
23 comment_approved: 1, // default is 1
24 comment_post_id: object.og_object.id // this you need to figure out, using the comment object's ID as placeholder for now
25 }
26 // console.info(commentEntry)
27 mappedComments.push(commentEntry)
28 })
29 }
30})
31// console.info('Comments', mappedComments)
32
33// Write the results (in JSON format) to a file
34fs.writeFile('comments.json', JSON.stringify(mappedComments), err => {
35 if (err) throw err
36 console.info('File is created successsfully')
37})
The comments that you extract need to be associated with a post ID in order
If the ID was previously in the URL, you can use regex to get it from that.
I had to figure out what the post ID (comment_post_id
) was by looking up the post title in the WordPress database