switch_to_blog() performance considerations & alternatives

I’m currently in the conceptual phase for a multisite network.

The rough idea is: There are multiple networked sites and all publish posts for their own. The network admin is able to establish “content links” between each of the blogs. A “content link” means, that the admin is able to configure the sharing of posts between the blogs in the backend. Either single (-->) or both way (<->). This results in the listing pages of a blog showing also posts from other blogs.

For example if we have three blogs the “content links” could be configured like this:

A <-> B

A –> C

This would result in:

A also shows the content from B

B also shows the content from A

C also shows the content from A and also from B (through ancestry relation B -> A -> C)

Personally I would like to stick with WP_Query(), as I like the features I will get with it:

  • pagination support,
  • taxonomy queries,
  • post_meta queries.

But it seems like I’m stuck with using switch_to_blog() massively if I want to use WP_Query(). The system has to be laid out for approx. 100 blogs with 200 posts each, which means in the worst case I have to make use of 99 calls to switch_to_blog() to render a single post list/category page.

Has anybody ever done massive content sharing between networked blogs or has an idea on howto to achieve a decent performance for this task?

1 Answer
1

You should first read (emphasis mine):

The sites in a multisite network are separate, very like the separate
blogs at WordPress.com. They are not interconnected like things in
other kinds of networks (even though plugins can create various kinds
of interconnections between the sites). If you plan on creating sites
that are strongly interconnected, that share data, or share users,
then a multisite network might not be the best solution
.

http://codex.wordpress.org/Before_You_Create_A_Network

If you insist on making multisite do what it is not intended to do, and instead do what it seems that most people want it to do, then you have a lot of work to do. You should be able to get WP_Query to honor your site switches via its numerous filters, especially these. And some function run their own queries, so you will have to deal with those individually.

It sounds like multisite is the wrong way to approach this. When I need something like this I tend to do it with custom post types. You’d create one CPT for each “blog”, though I have to admit that I’ve never tried to create 100 CPTs on the same site. The raw post total (20,000) shouldn’t be a problem though. With clever use of templates you can “virtualize” those into a visually nearly independent sites.

Given your comment below that you need “domain mapping, seperated userbases and seperate admin areas”, I am not sure that will work. You could probably get the domain mapping to work, but the separate userbases and admin areas would be a lot of work. That is one of the only other options I can think of though.

Leave a Comment