HowTo – WordPress 3 Sharing Content Between SubDomain Sites

Filed under: Wordpress Tips

wordpress-logoIn WordPress 3.0 you have access to the features available in previous versions of WordPress MU. This allows you to setup a main site and then provide SubDomain or SubDirectory child sites off of your main site.

There are many reasons to do this but sometimes you may want to setup a Parent and Child site that have relationships. For instance you could have a main news site and then have ChildSites that provide content that you display as blocks on the ParentSite.

Now it is possible to read posts into the parent site but if updates are made they won’t be automatically changed on the parent site if they are imported through a db call or through a rss feed. What you are trying to do is display content not harvest it.

Since every site that is made in WordPress 3.0 is independent you will need a way to switch into the other blog’s content and grab information.

This can be done within your theme by using switch_to_blog.

In order to switch to the correct site to grab the correct data you will need to login as superadmin and get the id number of the site.

Now you can edit your Theme file … most likely your home.php to include a loop of posts or any other information that may be available .

[php][/php]

<?php

$other_blog="9"; #9 is the id of the site we will change to

switch_to_blog($other_blog);

#DO SOMETHING

restore_current_blog(); #you just switched back

?>

[php][/php]

It is really not that difficult and you won’t need to generate any special mysql calls to your database to get data. Just retrieve the data in the same way you normally would.

You can get pretty fancy working in this method and it allows you to delegate content management to others instead of dumping it all into one mass site.

One note is that you probably want to make sure your vars are correct and use specific naming conventions so you don’t endup mixing content.