Skip to content

Instantly share code, notes, and snippets.

@trey8611
Forked from mbissett/migrating-bbpress-content.md
Last active October 29, 2024 16:58
Show Gist options
  • Save trey8611/fa38f6874f2b1ff5c3107fa716a2b2e7 to your computer and use it in GitHub Desktop.
Save trey8611/fa38f6874f2b1ff5c3107fa716a2b2e7 to your computer and use it in GitHub Desktop.
Migrating bbPress content with WP All Export and WP All Import

Introduction

Exporting/importing bbPress data presently requires some custom code that utilizes our API: http://www.wpallimport.com/documentation/developers/action-reference/.

Below, we have a general guide on how to do a basic forum/topics/replies migration. Keep in mind that this is only an example and you may need to modify the approach/code to make it work exactly as desired.

Code

Save the following code in the Function Editor (All Import -> Settings):

add_action( 'pmxi_saved_post', 'soflyy_forum_data', 10, 3 );

function soflyy_forum_data( $post_id, $xml_record, $is_update ) {
	global $wpdb;
	$import_id = wp_all_import_get_import_id();
	$post_type = wp_all_import_get_import_post_type( $import_id );

	switch ( $post_type ) {
		case 'topic':
			$parent_forum = get_post_meta( $post_id, '_bbp_forum_id', true );
			if ( empty( $parent_forum ) ) return;

			$forum = $wpdb->get_var( "SELECT `post_id` FROM `" . $wpdb->prefix . "postmeta` WHERE `meta_key` = '_bbp_old_forum_id' AND `meta_value` = '" . $parent_forum . "'" );
			if ( ! $forum ) return;

			update_post_meta( $post_id, '_bbp_topic_id', $post_id );
			update_post_meta( $post_id, 'post_alter_id', $post_id );
			update_post_meta( $post_id, '_bbp_last_active_time', date( "Y-m-d H:i:s", time() ) );
			update_post_meta( $post_id, '_bbp_forum_id', $forum );
			
			$args = array(
				'ID'          => $post_id,
				'post_parent' => $forum
			);
			wp_update_post( $args );
			break;

		case 'reply':
			$old_topic_id = get_post_meta( $post_id, '_bbp_topic_id', true );
			if ( empty( $old_topic_id ) ) return;

			$topic = $wpdb->get_var( "SELECT `post_id` FROM `" . $wpdb->prefix . "postmeta` WHERE `meta_key` = '_old_topic_id' AND `meta_value` = '" . $old_topic_id . "'" );
			if ( ! $topic ) return;

			update_post_meta( $post_id, '_bbp_topic_id', $topic );
			
			$forum = $wpdb->get_var( "SELECT `post_id` FROM `" . $wpdb->prefix . "postmeta` WHERE `meta_key` = '_bbp_old_forum_id' AND `meta_value` = '" . get_post_meta( $post_id, '_bbp_forum_id', true ) . "'" );
			if ( ! $forum ) return;

			update_post_meta( $post_id, '_bbp_forum_id', $forum );

			$args = array(
				'ID' => $post_id,
				'post_parent' => $topic
			);
			wp_update_post( $args );
			break;

		default:
			return;
	}
}

Migrate Forums

Use WP All Export's "Migrate" feature to get all of the data you need, then download the "Bundle" file. Upload the bundle to a new import, but don't skip to the final step. Contine to step 3 and add a new custom field called "_bbp_old_forum_id" with the value being the {id[1]} import element.

Migrate Topics

In the import, make sure the Forum ID is stored in a custom field named "_bbp_forum_id". And, add a new custom field called "_old_topic_id" with the value being the {id[1]} import element.

Migrate Replies

Make sure the "_bbp_forum_id" and "_bbp_topic_id" custom fields are being imports with the {_bbp_forum_id[1]} and {_bbp_topic_id[1]} import elements.

Finishing up

This may not always be necessary, but if the data on the front end of the forum looks off, you should try going to Tools -> Forums and run a repair on any of the data that looks incorrect.

@webdarren78
Copy link

Just to check - when it says:
make sure the Forum ID is stored in a custom field named "_bbp_forum_id"

Is this the old forum ID?

@trey8611
Copy link
Author

Is this the old forum ID?

Yes.

@iam39
Copy link

iam39 commented Oct 29, 2024

I'm trying to migrate a forum, I've managed to migrate the forum and topics with minimal issues, I just had to reassign the topics to the forum and the correct author, fortunately there is only 1 forum and 8 topics. I'm trying to migrate the replies, even tough I checked the import settings, it always imports every reply as title "Reply To:" forum "(Mismatch)" and defaults the author to me.
Any ideas?
I don't see anything in this code which seems to effect replies, just seems relevant to the forums and topics?
Thanks
Dan

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment