Sync Local and Remote AnsPress WP

7.35K viewsWordPress
0

Hi Rahul,

I was thinking if there is a way to sync both local and remote wordpress.

As I had earlier mailed you, I am looping through external database (which contains multiple choice questions with answers), to create AnsPress Questions (custom wordpress post). So it would be easier for me to create them on localhost and then somehow send these Questions on live site.

(sorry for the off-topic question)

Regards,
Atul

lol… I originally posted this question as a private, but after editing a typo, this question automatically became public. is it an issue on editing a private question or something I missed, @Rahul ?

Can you perhaps share your code on importing? I’m looking for a script I can easily modify to get my question2answer website into Anspress for WordPress

@acidrazor please see the piece of code on pastebin -http://pastebin.com/yeSdxtaK
Although as Rahul has mentioned in his reply that he will be working on migrate script soon. That’s a good news.

@atul after you posted comment avatar got fixed 😐 really weird issue

@rahul, No. it was not corrected after posting comment. Rather it was corrected after I edited the answer. i guess well someday you’ll find
1. why this happened,
2. my private question became public after editing (some more small issues i will write somewhere else :P)

need to investigate 🙂 anyway thanks fro reporting the issue

0

@AcidRaZar, I don’t know if it will be any useful to you, but this is my updated script to import Question and Answer from my external Q&A database.
Since my Q&A database is linear (same row contains a question and its answer column contains the answer) unlike Q2A database structure (which is question in one row, then answers in another multiple rows linked to parent question row; defined by type of row – either a question/answer/comment).
So, you need to edit code at some relevant places, such as loop until you get all answers of a particular question. (edit according to Q2A database).

It will hardly take 1 or 2 days to make your own (raw) import – script, hard-coded for your database only.

Note – 1. Let Rahul comment on any missing answers-meta, as I don’t know about them.

<?php
// replace your connection settings
$db = @mysql_connect('localhost', 'root', 'password') or die(mysql_error());
mysql_select_db('database_name', $db) or die(mysql_error());
$qry_qbank = mysql_query("SELECT * FROM table_name limit 100") or die(mysql_error()); 

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

define('ANSPRESS_VOTE_META', '_ap_vote');
define('ANSPRESS_FAV_META', '_ap_favorite');
define('ANSPRESS_CLOSE_META', '_ap_close');
define('ANSPRESS_FLAG_META', '_ap_flag');
define('ANSPRESS_VIEW_META', '_views');
define('ANSPRESS_UPDATED_META', '_ap_updated');
define('ANSPRESS_ANS_META', '_ap_answers');
define('ANSPRESS_SELECTED_META', '_ap_selected');
define('ANSPRESS_BEST_META', '_ap_best_answer');
define('ANSPRESS_PARTI_META', '_ap_participants');

define('AP_FOLLOWERS_META', '_ap_followers');
define('AP_FOLLOWING_META', '_ap_following');



while($row = mysql_fetch_array( $qry_qbank )) {
$qno = $row['id'];
$question_title = $row['Question_Title'];

$question_content = $row['Question_Content'];

$answer_content = $row['Answer_Content'];;

$category = array(12, 13, 15);
$tags = array(21, 22, 23);
$user_id = 1;

$status = 'publish';

$question_array = array(
    'post_title' => $question_title,
    'post_author' => $user_id,
    'post_content' => wp_kses($question_content, ap_form_allowed_tags()),
    'post_type' => 'question',
    'post_status' => $status
);

$question_post_id = wp_insert_post($question_array);

if ($question_post_id) {
    
    wp_set_post_terms($question_post_id, $category, 'question_category');
    wp_set_post_terms($question_post_id, $tags, 'question_tags');

    update_post_meta($question_post_id, ANSPRESS_VOTE_META, '0');
    update_post_meta($question_post_id, ANSPRESS_FAV_META, '0');
    update_post_meta($question_post_id, ANSPRESS_CLOSE_META, '0');
    update_post_meta($question_post_id, ANSPRESS_FLAG_META, '0');
    update_post_meta($question_post_id, ANSPRESS_VIEW_META, '0');
    update_post_meta($question_post_id, ANSPRESS_UPDATED_META, current_time('mysql'));
    update_post_meta($question_post_id, ANSPRESS_SELECTED_META, false);
    
    ap_add_parti($question_post_id, $user_id, 'question');
    
    update_post_meta($question_post_id, ANSPRESS_ANS_META, '0');

    do_action('ap_after_inserting_question', $question_post_id);
    ap_do_event('new_question', $question_post_id, $user_id);
    
    $question_permalink = get_permalink($question_post_id);
	
				$ans_array = array(
				'post_author'	=> $user_id,
				'post_content' 	=> wp_kses($answer_content, ap_form_allowed_tags()),
				'post_type' 	=> 'answer',
				'post_status' 	=> 'publish',
				'post_parent' 	=> $question_post_id
			);
			$answer_post_id = wp_insert_post($ans_array);
			
			if($answer_post_id){
			$answer_permalink = get_permalink($answer_post_id);
			}
}
echo "<a href='$question_permalink'>question - {$qno}/{$question_post_id} - link</a>";
echo "<br/>";
echo "<a href='$answer_permalink'>answer - {$qno}/{$answer_post_id} - link</a>";
echo "<br/>";
}

Regards,
Dr. Atul Tiwari

Code looks good to me 🙂

@Rahul, thanks for reviewing it. I posted updated code, after AcidRazor’s comment.
I think, now is good time (after working on open-wp layout) that you should work on notification system and mentions (@), as sometimes (mostly) comments are missed, when multiple answers and comments are posted since last logged in, as activity log displays only one last activity.

Thanks, but I think I’ll still wait for an official script. I need my users transferred as well as comments on questions including their answers as with the Q2A database layout

You are viewing 1 out of 3 answers, click here to view all answers.