Skip to content

Instantly share code, notes, and snippets.

@mbissett
Last active December 8, 2020 21:20
Show Gist options
  • Save mbissett/3a8ee3f44b6919feca59afdd4603a703 to your computer and use it in GitHub Desktop.
Save mbissett/3a8ee3f44b6919feca59afdd4603a703 to your computer and use it in GitHub Desktop.
Migrating bbPress content

Exporting/importing bbPress data presently requires some custom code that utilizes our API: http://www.wpallimport.com/documentation/developers/action-reference/. The general steps you'd need to take are:

  1. Export the forums, topics, and replies separately.
  2. Import the Forums before you import the Topics & Replies. In the Topics import, put the Forum ID into a custom field named "_bbp_forum_id", and use the pmxi_saved_post hook along with wp_update_post to set the post_parent to the forum ID. With the same hook, you'll have to update the '_bbp_topic_id', 'post_alter_id', and '_bbp_last_active_time' fields with data in the format that bbPress requires.
  3. In the "Replies" import, set the '_bbp_topic_id' custom field to the Topic IDs that you previously imported. You'll probably have to use a custom PHP function to find the new topic IDs.

Here's a really basic example snippet that should give you an idea of what's needed:

<?php
add_action( 'pmxi_saved_post', 'soflyy_forum_data', 10, 3 );

function soflyy_forum_data( $post_id, $xml_record, $is_update ) {
	$import_id = ( isset( $_GET['id'] ) ? $_GET['id'] : ( isset( $_GET['import_id'] ) ? $_GET['import_id'] : 'new' ) );
	
	if ( $import_id == '4' ) {
		// Topics import
		$parent_forum = get_post_meta( $post_id, '_bbp_forum_id', true );
		if ( $parent_forum != 0 ) {
			$args = array(
				'ID' => $post_id,
				'post_parent' => $parent_forum
				);
			wp_update_post( $args );
			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() ) );
		}
	} elseif ( $import_id == '5' ) {
		// Replies import
		$old_topic_id = get_post_meta( $post_id, '_bbp_topic_id', true );
		$post = get_posts( array(
			'post_type' => 'topic',
			'meta_key' => '_old_topic_id',
			'meta_value' => $old_topic_id
			) );
		if ( $post ) {
			$new_topic_id = $post[0]->ID;
			update_post_meta( $post_id, '_bbp_topic_id', $new_topic_id );
		}
	}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment