函数文档

wp_update_network_site_counts()

💡 云策文档标注

概述

wp_update_network_site_counts() 函数用于更新网络范围内的站点计数。它通过查询指定网络中的非垃圾、未删除、未归档站点数量,并将结果存储为网络选项。

关键要点

  • 函数接受一个可选的 $network_id 参数,默认为当前网络 ID。
  • 使用 get_sites() 查询符合条件的站点数量,参数包括 network_id、spam、deleted、archived 等。
  • 通过 update_network_option() 将计数更新到网络选项 'blog_count' 中。
  • 自 WordPress 4.8.0 起添加了 $network_id 参数,允许指定网络。

代码示例

function wp_update_network_site_counts( $network_id = null ) {
	$network_id = (int) $network_id;
	if ( ! $network_id ) {
		$network_id = get_current_network_id();
	}

	$count = get_sites(
		array(
			'network_id'             => $network_id,
			'spam'                   => 0,
			'deleted'                => 0,
			'archived'               => 0,
			'count'                  => true,
			'update_site_meta_cache' => false,
		)
	);

	update_network_option( $network_id, 'blog_count', $count );
}

注意事项

  • 该函数主要用于多站点网络环境,更新站点计数以反映当前状态。
  • 相关函数包括 wp_update_network_counts() 和 wp_maybe_update_network_site_counts(),用于更广泛的网络计数更新。

📄 原文内容

Updates the network-wide site count.

Parameters

$network_idint|nulloptional
ID of the network. Default is the current network.

Default:null

Source

function wp_update_network_site_counts( $network_id = null ) {
	$network_id = (int) $network_id;
	if ( ! $network_id ) {
		$network_id = get_current_network_id();
	}

	$count = get_sites(
		array(
			'network_id'             => $network_id,
			'spam'                   => 0,
			'deleted'                => 0,
			'archived'               => 0,
			'count'                  => true,
			'update_site_meta_cache' => false,
		)
	);

	update_network_option( $network_id, 'blog_count', $count );
}

Changelog

Version Description
4.8.0 The $network_id parameter has been added.
3.7.0 Introduced.