wp_2_posts这类分表之上,再建一张全局的wp_content_hub汇总表,只存ID、标题、摘要、缩略图URL、原文链接、发布时间、站点ID七字段,轻量级、可水平扩展。/wp-json/custom/v1/feed,每个子站点只推送增量内容;主站通过wp_remote_get批量抓取,写入汇总表,并触发增量索引到Elasticsearch。content_hash,更新即换URL,避免穿透。wp-config.php追加:define('WP_ALLOW_MULTISITE', true);wp-config.php与.htaccess,完成子站点创建。CREATE TABLE wp_content_hub (
id BIGINT AUTO_INCREMENT,
blog_id INT NOT NULL,
post_id BIGINT NOT NULL,
title TEXT,
excerpt TEXT,
thumb VARCHAR(255),
permalink VARCHAR(255),
post_date DATETIME,
PRIMARY KEY(id),
UNIQUE KEY uniq_post (blog_id, post_id)
) ENGINE=InnoDB;functions.php注册路由:add_action('rest_api_init', function () {
register_rest_route('custom/v1', '/feed', 【
'methods' => 'GET',
'callback' => 'send_latest_posts',
】);
});
function send_latest_posts() {
$posts = get_posts(【
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 50,
'orderby' => 'modified',
'order' => 'DESC',
】);
$data = 【】;
foreach ($posts as $p) {
$data【】 = 【
'blog_id' => get_current_blog_id(),
'post_id' => $p->ID,
'title' => $p->post_title,
'excerpt' => wp_trim_words($p->post_content, 40),
'thumb' => get_the_post_thumbnail_url($p->ID, 'medium'),
'permalink' => get_permalink($p->ID),
'date' => $p->post_modified,
】;
}
return rest_ensure_response($data);
}wp-content/mu-plugins新建sync-hub.php:function sync_hub_daily() {
$blogs = get_sites(【'number' => 100】);
foreach ($blogs as $b) {
$url = 'https://' . $b->domain . $b->path . 'wp-json/custom/v1/feed';
$res = wp_remote_get($url, 【'timeout' => 10】);
if (!is_wp_error($res)) {
$items = json_decode(wp_remote_retrieve_body($res), true);
foreach ($items as $it) {
global $wpdb;
$wpdb->replace('wp_content_hub', 【
'blog_id' => $it【'blog_id'】,
'post_id' => $it【'post_id'】,
'title' => $it【'title'】,
'excerpt' => $it【'excerpt'】,
'thumb' => $it【'thumb'】,
'permalink' => $it【'permalink'】,
'post_date' => $it【'date'】,
】);
}
}
}
}
// 每5分钟跑一次
if (!wp_next_scheduled('sync_hub_daily')) {
wp_schedule_event(time(), 'five_minutes', 'sync_hub_daily');
}
add_action('sync_hub_daily', 'sync_hub_daily');functions.php加:add_filter('cron_schedules', function ($s) {
$s【'five_minutes'】 = 【'interval' => 300, 'display' => '每5分钟'】;
return $s;
});template-hub.php:$items = $wpdb->get_results("SELECT * FROM wp_content_hub ORDER BY post_date DESC LIMIT 50");
foreach ($items as $it) {
echo '' ;
echo '. esc_url($it->permalink) . '">';
echo '
. esc_url($it->thumb) . '">';
echo ''
. esc_html($it->title) . '';
echo ''
. esc_html($it->excerpt) . '';
echo '';
echo '';
}wp-config.php追加:define('WP_REDIS_HOST', '127.0.0.1');
define('WP_CACHE', true);sync_hub_daily里加入Prometheus指标推送:if (class_exists('Prometheus\CollectorRegistry')) {
$registry = \Prometheus\CollectorRegistry::getDefault();
$counter = $registry->getOrRegisterCounter('wp', 'sync_total', 'Synced posts', 【'blog'】);
$counter->inc(【$it【'blog_id'】】);
}template-home-old.php,在Nginx层用map $cookie_test_group $template实现灰度,一旦异常,修改map值即可切换。数据库中的“limit”魔法,轻松掌握SQL Server数据筛选
WordPress多站点内容聚合策略与实践
幽默指南:如何轻松备份你的迷你 MySQL 数据库
双服务器同步备份信息:轻松掌握数据安全
WordPress 里程插件:数据驱动的精准里程计算
让iframe像钱包一样自动伸缩的WordPress教程
云端备份,快乐无忧
WordPress 里程插件:数据驱动的精准里程计算
让iframe像钱包一样自动伸缩的WordPress教程
轻松打造互动留言板:老板教你WordPress留言板设计秘籍
解决WordPress迁移后403错误:权限、索引与安全策略
幽默教程:如何用地区限制插件让外省网友远离你的WordPress直播
兼容模式:WordPress的老布鞋囧事
水印技术:WordPress插件的图像版权保护方案
WordPress悬浮按钮轻松添加全教程
电商地图插件:WordPress地图插件的实战应用指南
王总分享内网穿透WordPress的开心经历
怒斥内网穿透:简单三步,告别WordPress访问难题
WordPress网站首次加载速度优化指南