Shared database wordpress: Posts table

I was looking for a way to use the same posts table for two seperate WordPress installations. The following will only work if they are using the same database. Turned out to be quite easy. All you have to do is edit the wp-db.php in the includes directory.

In that file locate the following function:

	function tables( $scope = 'all', $prefix = true, $blog_id = 0 ) {
		switch ( $scope ) {
			case 'all' :
				$tables = array_merge( $this->global_tables, $this->tables );
				if ( is_multisite() )
					$tables = array_merge( $tables, $this->ms_global_tables );
				break;
			case 'blog' :
				$tables = $this->tables;
				break;
			case 'global' :
				$tables = $this->global_tables;
				if ( is_multisite() )
					$tables = array_merge( $tables, $this->ms_global_tables );
				break;
			case 'ms_global' :
				$tables = $this->ms_global_tables;
				break;
			case 'old' :
				$tables = $this->old_tables;
				break;
			default :
				return array();
				break;
		}

		if ( $prefix ) {
			if ( ! $blog_id )
				$blog_id = $this->blogid;
			$blog_prefix = $this->get_blog_prefix( $blog_id );
			$base_prefix = $this->base_prefix;
			$global_tables = array_merge( $this->global_tables, $this->ms_global_tables );
			foreach ( $tables as $k => $table ) {
				if ( in_array( $table, $global_tables ) )
					$tables[ $table ] = $base_prefix . $table;
				else
					$tables[ $table ] = $blog_prefix . $table;
				unset( $tables[ $k ] );
			}

			if ( isset( $tables['users'] ) && defined( 'CUSTOM_USER_TABLE' ) )
				$tables['users'] = CUSTOM_USER_TABLE;

			if ( isset( $tables['usermeta'] ) && defined( 'CUSTOM_USER_META_TABLE' ) )
				$tables['usermeta'] = CUSTOM_USER_META_TABLE;
		}

		return $tables;
	}

Now add the following lines above the CUSTOM_USER_TABLE line:

	$tables['posts'] = 'wp_posts';
	$tables['postmeta'] = 'wp_postmeta';

You will need to the change the ‘wp_’ to the prefix you chose at installation time. Now you can edit the same posts in both instances. The following allows you to share taxonomy tables as well:

	$tables['term_taxonomy'] = 'wp_term_taxonomy';
	$tables['term_relationships'] = 'wp_term_relationships';
	$tables['terms'] = 'wp_terms';

And you’ll need to share comments as well ofcourse:

	$tables['comments'] = 'wp_comments';
	$tables['commentmeta'] = 'wp_commentmeta';

Keep in mind that the change will be overwritten anytime you update it. So keep a copy to replace it again. I haven’t noticed any bugs or strange behaviour so far but i’ve only been using it a day or two. This is a hack and thus can cause problems with your installation. Do not attempt this without a backup.

Leave a Reply

Your email address will not be published. Required fields are marked *

*