Publish Posts to All Sites in WordPress Multisite

Actually, I’m about to show you not only how to publish posts to all sites in your WordPress multisite network but also how to sync the changes made in those posts between sites. In other words, let’s talk about ways of setting up shared content between sites in a multisite network.

There are two ways of how you can accomplish that:

  1. When a post is published or updated on a main site of your network (you can decide yourself which one is going to be main or simply use the get_main_site_id() function), it will be automatically published or updated on all sites in the multisite network. In the first method, we’re going to do it programmatically and use save_post action hook to trigger that.
  2. The second method is similar to the first one; however, the difference is that you can decide which sites of the multisite network you’re going to publish posts to or sync their changes. And we’re going to use a WordPress plugin – Simple Multisite Crossposting, in that case.

Oh, yes, there is also a third way, which is completely different – the idea is indexing content and then having access to it anytime from any site; my other plugin can help you with that.

1. Post to All Sites in a WordPress Multisite Network Programmatically

Let’s begin with the programmatic way, but you can just skip this chapter and continue straight to the plugin way. By the way, the code below is similar to the code we used in this tutorial when moving network posts (or pages) from one site to another.

As you may probably guess, the first thing we need to do here is to decide which one of the sites is going to be the main one. We can do it manually or use the get_main_site_id() function, for example. From this specific site, we’re about to automatically post to all the other network sites.

Below is the snippet that you can use either on the main site of your network only or activate it network-wide.

/*
 * WordPress Multisite: Post to all sites at once
 *
 * @author Misha Rudrastyh
 * @link https://rudrastyh.com/wordpress-multisite/post-to-all-sites.html
 */
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;
	}

	// actually we need only "publish" status
	if( 'publish' !== get_post_status( $original_post ) ) {
		return;
	}
	
	// here you can check whether you're on the main site for example
	$main_site_id = get_main_site_id();
	if( $main_site_id !== get_current_blog_id() ) {
		return;
	}

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

	// time to get all sites
	$site_ids = get_sites( 
		array(
			'fields' => 'ids',
			'site__not_in' => array( $main_site_id ),
			'number' => 50, // 100 by default
		)
	);

	// 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
	$post_terms = wp_get_object_terms( $original_post_id, 'category', array( 'fields' => 'slugs' ) );
	$post_meta = get_post_custom( $original_post_id );

	foreach( $site_ids as $site_id ) {

		switch_to_blog( $site_id );

		// if a 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' => 'any' ) ) ) {
			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();

	}

}

Some notes:

  • Our code shouldn’t duplicate drafts, so I added a get_post_status() condition. But if you need to sync drafts as well, you can skip that condition.
  • Using remove_action( 'save_post' ... is critical; otherwise, the function will be called inside itself many times because we’re using wp_insert_post() which also has a save_post hook inside. As a result, the fatal error: “Maximum function nesting level reached.”
  • I am also using the get_sites() function because we decided that we’re going to post to all sites in a multisite network at once, but, of course, you can just provide blog IDs as an array instead. As you can see, I used a site__not_in parameter to exclude the current site, obviously.
  • The rest of the code is a little bit simplified; I just get post categories and custom fields and add them to cross-posted posts as well, but what if you have some custom taxonomies? And how about featured images? If you need a complete solution, please check the plugin below.

2. A Plugin for WordPress Multisite that Allows You to Post to All Sites

It is time to talk about the plugin approach.

In this chapter, we will talk about my Simple Multisite Crossposting plugin.

At this moment, the plugin supports almost everything you can imagine: custom post types and taxonomies, featured images, ACF and similar metabox plugins, WooCommerce products, pages created with Elementor, and so on.

Here is what it looks like in the WordPress Block editor:

post to multiple sites in WordPress multisite network
It is how my plugin allows you to publish a post to multiple sites in a multisite network at once.

The question you might have at the moment: “Wait, but we need to select sites using these checkboxes; is there a way to automatically post to multiple sites?” The answer is “Yes!”. In that case, you just need to turn on the “Auto mode” option in the plugin settings.

WordPress multisite post to all sites automatically

Have any questions left? Let me know in the comments below.

Misha Rudrastyh

Misha Rudrastyh

Hey guys and welcome to my website. For more than 15 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 X