多服务器部署的WordPress站点媒体库同步问题咨询
Hey Keith, great question—since you're already sharing the wp-content/uploads folder, the file sync part is mostly covered, but WordPress stores critical media metadata (like attachment titles, alt text, and links) in the database, so we need to address both layers to get full media library sync across your three sites. Here are the most practical approaches tailored to your setup:
Since your codebase is identical across sites (only environment variables differ), the core issue is syncing the attachment-related records in the WordPress database.
Scripted Database Export/Import (Low-Cost, Reliable)
Usemysqldumpto export only the media-related records from your Live site, then import them to Beta and Kiosk. This avoids syncing unrelated post data.- Example export command (targets only attachments):
mysqldump -u your_db_user -p your_live_db wp_posts wp_postmeta --where="wp_posts.post_type='attachment'" > media_sync.sql - Before importing to Beta/Kiosk, update domain references if your sites use different URLs:
sed -i 's/https://your-live-domain.com/wp-content/uploads/https://your-beta-domain.com/wp-content/uploads/g' media_sync.sql - Schedule this with
cronto run daily (or more frequently) for automatic sync.
- Example export command (targets only attachments):
WordPress Plugin Workflow (User-Friendly)
Plugins like WP All Import/Export let you export a dedicated XML feed of your Live site's attachments, then set up scheduled imports on Beta and Kiosk. This is great if you prefer a GUI over command-line tools, though it’s less performant for large media libraries.
If you want to avoid syncing databases entirely, modify your Beta and Kiosk sites to pull media metadata directly from the Live site’s database.
Add this snippet to your Beta/Kiosk wp-config.php (or a custom plugin):
// Route media library queries to the Live database add_action('pre_get_posts', function($query) { // Only target admin media library queries if (is_admin() && $query->get('post_type') === 'attachment') { global $wpdb; // Replace with your Live database credentials $live_db = new wpdb( 'live_db_user', 'live_db_password', 'live_db_name', 'live_db_host' ); $wpdb = $live_db; } });
This makes Beta/Kiosk admins see the exact media library as Live, while still using their own databases for other content. Just ensure your Beta/Kiosk database users have read access to the Live database.
If you’re using a network mount (like NFS or SMB) for wp-content/uploads, double-check:
- The mount is bidirectional (all sites can read/write to the folder) so new uploads from any site appear everywhere.
- File permissions are consistent across servers (use
chmod 755for folders and644for files) to avoid WordPress permission errors.
- Centralize Uploads: Restrict media uploads to the Live site only. This eliminates sync conflicts and means you only need to push updates to Beta/Kiosk, not pull from them.
- Fix Cross-Domain Links: Use the WordPress CLI to update URL references in metadata:
wp search-replace 'https://live-domain.com/wp-content/uploads' 'https://kiosk-domain.com/wp-content/uploads' --skip-columns=guid - Test Syncs: Run manual syncs first to verify metadata and files align before setting up automation.
内容的提问来源于stack exchange,提问作者Keith Pickering

