Sync Local and Remote AnsPress WP

6.57K 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

1

Very simple answer and very simple solution 😀
PhpMyAdmin have this feature, search for “PhpMyAdmin local and remote sync”

Thanks for mentioning it, i will try that 🙂

@Rahul, after trying this, I recall now that what issues I had with this answer –

1. First of all I am NOT at all able to connect to remote database (I am sure that I am filling info correctly, because I am using the same in my android phone mysql app, and it’s working fine there with the same connection info) – error i get is – “Could not connect to the target”

2. I thought – if there will be any broken links in wordpress, if I get success in synching by phpmyadmin (don’t know it’s result as first I have not got success in syncing, and second wordpress is too complex for me understand quickly).

anyways i will keep trying and let you know, weather or not i was successful.

@Rahul – update to my previous comment – i found my mistake in remote connection to phpmyadmin.
Also, I tested a plugin – WP Sync DB, it’s good too. Although I need to learn wordpress and anspress little bit in-depth… hope someday I will 🙂 gn.

1

Well, this is NOT the answer to my original question.
@AcidRaZor, this is the piece of code I am using (and it’s NOT complete). It’s similar to what wordpress help shows on how to make wp-post in PHP.

After posting here, My code was automatically modified for unknown reasons. So I have pasted it on pastebin. It’s link is – http://pastebin.com/yeSdxtaK. Please check it there.

<?php
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');

$question_title = "This space is for Question Title...";
$user_id = 1;
$question_description = "This space is for actual/detailed question post";
$category = array(12, 13, 15);
$tags = array(21, 22, 23);

$status = 'publish';

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

$post_id = wp_insert_post($question_array);

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

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

    do_action('ap_after_inserting_question', $post_id);
    ap_do_event('new_question', $post_id, $user_id);
    
    $question_permalink = get_permalink($post_id);
}

echo "<a href='$question_permalink'>link</a>";

p.s. –
1. it will only create question. I had not created for posting answers. but I think $answer_array can be created in similar manner and use that array into wp_insert_post inside if ($post_id) block
2. I am NOT a programmer. so, it may be a worst piece of code, one can see in his life-time. but if there is any other way, please share it.

Regards,
Dr. Atul Tiwari

as a non programmer you did a good try 🙂 I will make a migration within 1-2 days.

did you finish the script? I want to migrate Q2A please

@acidRaZor, check my newly posted answer

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