Adventskalender Day 23 – How to update custom fields in a multi-site

One of the nice things about a multi-site is the possibility to create relationships between posts across the network. For example you might use a private blog in your network to discuss editorial issues and link these conversations to the discussed posts.

Or you create a multi-language network and mark a post on as a translation for a post on and You want to provide the post editor for the linked posts, so you can make edits for all of them on one page.

All you need is a simple table for relationships and a routine to update the connected posts, whenever one of them is changed. You will probably use switch_to_blog() and wp_update_post() for that.

It could look like this pseudo-code:

And then you will discover how many plugins are built for single-site installations only and mess up your posts on a multi-site when they are activated network-wide. Why? Because they update the post meta data for all posts without any test to see if they are on the same site as their metaboxes. switch_to_blog() is an unknown factor for most plugins.

The results make users struggle hard: Russian SEO key words are applied to the English site without any notice and no easy way to fix. Strange subtitles appear o the linked posts, or notes meant to stay hidden leak from the editorial blog to the public. And when they change it on the affected posts, all other posts are updated again, this time with new, but still wrong data.

There is a simple fix: pass the source blog’s ID with your data. In your metabox, add a hidden field …

… and in your request validation, before you save any data:

And that’s all. Your users are happy, you will not cause data loss anymore. Even when two plugins use the same field name for this, it will work.

Post Sharing

Author Avatar

I am a developer and author for MarketPress. In my free time, I am a moderator for several WordPress communities.

Also Interesting

New Plugin: Slack Connector - Connect WordPress, WooCommerce and Slack

by Michael Firnkes

Initially we merely wanted to optimize our own Slack-processes. With automated notifications from our MarkettPress shop, the blog and our forums. The resul ...

Read more

Inpsyde is the first VIP partner in Germany

by Michael Firnkes

Automattic has given us the title of VIP Service Partner, the first in Germany, Austria and Switzerland. Worldwide, there are only eleven com ...

Read more

Inpsyde is a WooExperts Gold Partner of WooCommerce

by Michael Firnkes

A few weeks ago, Inpsyde GmbH with MarketPress became a certified WooExperts Gold Partner. This is confirmation from WooThemes of our agency’s know-h ...

Read more

BackWPup Pro: Secure WordPress Backup with Google Drive

by Michael Firnkes

The Pro version of our BackWPup plugin supports the backup of WordPress databases and files to Google Drive. But how do you set something like that up? And ...

Read more