Publish Posts to Multiple Blogs in WordPress Multisite

There are actually two ways of organising shared content between sites in Multisite Network. The first way is all about publishing content to several blogs at once and then synchronising any changes (it is what we are going to talk in this tutorial). The second way is about indexing content and then having access to it anytime from any site (if you’re interested in the second way, please have a look at this plugin of mine).

save_post – it is actually all we need

By the way the code below is similar to the code we used in this tutorial to move network posts from one website to another.

add_action( 'save_post', 'misha_post_to_all_sites', 20, 2 );

function misha_post_to_all_sites( $original_post_id, $original_post ){

	// do not publish revisions
	if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
		return $original_post_id;
	}

	// actually we need only "publish" status
	if( 'publish' !== get_post_status( $original_post ) ) {
		return $original_post_id;
	}

	// prevent "Fatal error: Maximum function nesting level reached"
	remove_action( 'save_post', __FUNCTION__ );

	// here you have to specify blog IDs where you would like to publish the posts
	$blog_ids = array( 2, 3 );

	// let's get this post data as an array
	$post_data = array(
		'post_author' => $original_post->post_author,
		'post_date' => $original_post->post_date,
		'post_modified' => $original_post->post_modified,
		'post_content' => $original_post->post_content,
		'post_title' => $original_post->post_title,
		'post_excerpt' => $original_post->post_excerpt,
		'post_status' => 'publish',
		'post_name' => $original_post->post_name,
		'post_type' => $original_post->post_type,
	);


	// terms and post meta as well
	$post_terms = wp_get_object_terms( $original_post_id, 'category', array( 'fields' => 'slugs' ) );
	$post_meta = get_post_custom( $original_post_id );

	foreach( $blog_ids as $blog_id ) {

		switch_to_blog( $blog_id );

		// if post with the same slug exists, do nothing
		if( get_posts( array( 'name' => $post_data[ 'post_name' ], 'post_type' => $post_data[ 'post_type' ], 'post_status' => 'publish' ) ) ) {
			restore_current_blog();
			continue;
		}

		$inserted_post_id = wp_insert_post( $post_data );

		wp_set_object_terms( $inserted_post_id, $post_terms, 'category', false );
		
		foreach ( $post_meta as $meta_key => $meta_values) {
			// we do not need these redirects
			if( '_wp_old_slug' === $meta_key ) {
				continue;
			}
			foreach ( $meta_values as $meta_value ) {
				add_post_meta( $inserted_post_id, $meta_key, $meta_value );
			}
		}
		
		restore_current_blog();

	}

}
  • Our code shouldn’t duplicate drafts, so I added get_post_status() condition on line 9,
  • Line 12 is critical, otherwise, misha_post_to_all_sites() function will be called inside itself many times as we’re use wp_insert_post() in it. As a result, fatal error: “Maximum function nesting level reached”,
  • On line 15 you have to specify blog IDs where you would like to cross-publish posts. If you want to post to all sites, get_sites() function will help you to get all the blog IDs automatically excluding the current one,
  • On lines 32 and 47 you can see that I only use categories here, but you can also add the appropriate code for any terms you’re using within your Multisite Network.

Do not want to mess up with the code? Check another solution below.

Do You Need a Plugin for it?

Previously I asked in the comment section if any of you guys need a plugin for this. I received a lot of answers “Yes” and the plugin is ready!

It supports multiple post types and statuses, custom taxonomies and post thumbnails.

Here is how it looks in Gutenberg:

post to multiple subsite in WordPress multisite network
Here is how my plugin allows to publish a post to multiple blogs within WordPress Multisite.
Misha Rudrastyh

Misha Rudrastyh

Hey guys and welcome to my website. For more than 10 years I've been doing my best to share with you some superb WordPress guides and tips for free.

Need some developer help? Contact me

Follow me on Twitter