[Tutorial] How to Import Questions and Answers to AnsPress

7.70K viewsGeneral
1

Hi,

Thanks to Rahul, I successfully imported my Q&A database to AnsPress. Since I have seen this question a lot of times on AnsPress, I am posting how I did it. Hope it might help someone –

0. Backup everything you are about to change. If you are newbie like me, you never know, when and where silly mistakes are made.

1. Database structure – In my case, I converted my desired Q&A data into simple linear Q&A database, meaning single row contains Question, Answer, Category id, Tag id etc. in respective columns.

https://gist.github.com/rahularyan/e797c247e2b88b5b79a8

Column names are self-explanatory. (“AnsPress_Question_Id” was kept to insert anspress permanent link to the imported question. I had it for my further uses, not needed for most of the cases.)

2. Create a page in wordpress called import.

3. Create a new file called page-import.php inside active theme directory and paste the following code:

https://gist.github.com/atultiwari/cd9b5d00e54c238a02d1

Note –

a. Update database info (hostname, database name, username, password).

b. Modify code according to your database structure.

c. Check your Category Ids, Tags Ids and User Ids from AnsPress and update the code accordingly. (e.g. while importing I had to add +3 to category id, as this was the difference between my at_qa_dump and wordpress tables)

d. Un-comment following line if you are importing from wordpress root directory (i.e. not by creating page in theme directory)-

require('wp-blog-header.php');

4. Increase you PHP execution time : In my case, I changed php execution time to 50,000 (as my database was very large, 30K Q&A). PHP execution time of 5,000 imported approx 2500 Q&A in my first trial. To update it, search and edit following line your server’s php.ini file –

max_execution_time = 5000

5. Open the import page (e.g. http://localhost/your-wordpress-directory-path/import/) and let the query run. Sorry, not a programmer, so my code does not show real-time progress (slight lag is there, depending on number of your imported rows).

Known Issues –

1. In user’s profile page, answer text in answer list appears blank.

2. Importing is slow.

P.S. –

1. I am not at all a programmer. I just did this to make my site live again. All credits to Rahul Aryan, and thanks a lot for his continuous support and help.

2. Rahul and others feel free to make the changes in this procedure, so that it can be easier for others.

Thanks Dr.Atul. It will be helpful to other. I will extend this when I get some free time.

0

Hi, how did u converted your desired Q&A data into simple linear Q&A database?

Thanks

Hi, my actual database was of multiple choice questions with one or more correct answers. I converted it to linear (i.e. each mysql table row has only one question and one answer cell). If you are trying to import from Q2A or similar, where questions might have more than one answer posts then it’s not advisable to convert into linear database.
Instead I suggest you to use a secondary While or For loop inside “if ($question_post_id) ” segment (code posted in step three, line 32).
In short your first query would be to get question title and question description, then insert the question with wp_insert_post. Then, if successful execute query to get all answers of associated question-id where post type is answers ( or from your answers table, if it’s a separate table). Lastly insert the answers the same way.
You need to customize it as per your requirement. Not a techy, so don’t know if there is any easier way to do this. Sorry.

Thanks, well i use this plugin https://wordpress.org/plugins/wp-all-import/ and it works, export all my data to Anspress.

0

Hi,

 

I used WP ALL IMPORT too and it works fine for importing separately the tables I had in my original database : “Questions” and “Answers”. One question could receive several answers.

Yet every “Answer” row had a field called “fk__question” which was matching the Question’s ID in my database, so that the system knew which answers to assign to which question.

 

When trying to import this database into AnsPress I have all the answers imported but none is assigned to a question.

 

So if I’m not mistaken it boils down to what you said above, create a loop.

That would be something like this :

 

for each Answer, find‘fk__question’ and the Question it refers too, and import into wp_2_posts as a matching answer ‘Title’ and ‘Content” of the answer(s).

 

But how do I write that in code please ? I am no programmer either 🙂

Thanks a lot, thanks hugely for four help  !!