Automatically Crosspost Posts From a Specific Category Only

In the version 3.5 of Simple WordPress Crossposting “Auto Mode” feature was introduced which allows to publish posts automatically to all the added sites without selecting them for each posts manually.

But what if you would like to autopublish only specific posts? Let’s say from a specific category or tag? For that case I added rudr_crosspost_auto_mode_blogs filter hook which allows to configure you that.

First of all, please make sure that “Auto Mode” option is enabled in the plugin settings:

auto crossposting mode

Second, use a code snippet to configure auto crossposting:

add_filter( 'rudr_crosspost_auto_mode_blogs', function( $sites, $post_id ) {
	
	// if post is in category "Travel", only then we're crossposting it
	if( in_category( 'travel', $post_id ) ) {
		return $sites;
	}
	
	// you can use use has_tag() function to check post tags
	
	return array();
	
}, 20, 2 );

If you don’t know where to insert this piece of code, please check this guide or watch the video below:

There is also a possibility for more detailed configuration, for example if a post is in a specific category, you can auto-crosspost it to a specific site only.

add_filter( 'rudr_crosspost_auto_mode_blogs', function( $sites, $post_id ) {
	
	$allowed_sites = array();
	
	if( in_category( 'travel', $post_id ) ) {
		foreach( $sites as $site ) {
			if( 'https://rudrastyh.com' === $site[ 'url' ] ) {
				$allowed_sites[] = $site;
			}
		}
	}
		
	return $allowed_sites;
	
}, 20, 2 );

Now let’s take a look at a more complex example. What if you would like to crosspost to “Site 1” if “Category 1” is selected, to “Site 2” when “Category 2” is selected and to “Site 3” when “Category 3” is selected? The here is the snippet for you:

add_filter( 'rudr_crosspost_auto_mode_blogs', function( $sites, $post_id ) {
	
	$allowed_sites = array();
	
	// replace 'category-1' with your category slug
	if( in_category( 'category-1', $post_id ) ) {
		foreach( $sites as $site ) {
			// replace "Site 1 URL" with the website url with https://
			if( 'Site 1 URL' === $site[ 'url' ] ) {
				$allowed_sites[] = $site;
			}
		}
	}
	
	// replace 'category-2' with your category slug
	if( in_category( 'category-2', $post_id ) ) {
		foreach( $sites as $site ) {
			// replace "Site 2 URL" with the website url with https://
			if( 'Site 2 URL' === $site[ 'url' ] ) {
				$allowed_sites[] = $site;
			}
		}
	}
	
	// replace 'category-3' with your category slug
	if( in_category( 'category-3', $post_id ) ) {
		foreach( $sites as $site ) {
			// replace "Site 2 URL" with the website url with https://
			if( 'Site 3 URL' === $site[ 'url' ] ) {
				$allowed_sites[] = $site;
			}
		}
	}
		
	return $allowed_sites;
	
}, 20, 2 );

As a first parameter of in_category() function you can use category name, slug or ID.

Need more help?