函数文档

wp_maybe_update_network_site_counts_on_update()

💡 云策文档标注

概述

wp_maybe_update_network_site_counts_on_update() 函数用于在站点插入、更新或删除时,根据网络ID的变化更新网络站点计数。它通过调用 wp_maybe_update_network_site_counts() 来确保计数准确性。

关键要点

  • 函数接受两个参数:$new_site(必需,WP_Site 对象)和 $old_site(可选,默认为 null)。
  • 当 $old_site 为 null 时,直接更新 $new_site 的网络ID对应的站点计数。
  • 当 $new_site 和 $old_site 的网络ID不同时,更新两个网络ID的站点计数。
  • 函数在 WordPress 5.1.0 版本中引入。

代码示例

function wp_maybe_update_network_site_counts_on_update( $new_site, $old_site = null ) {
	if ( null === $old_site ) {
		wp_maybe_update_network_site_counts( $new_site->network_id );
		return;
	}

	if ( $new_site->network_id !== $old_site->network_id ) {
		wp_maybe_update_network_site_counts( $new_site->network_id );
		wp_maybe_update_network_site_counts( $old_site->network_id );
	}
}

📄 原文内容

Updates the count of sites for a network based on a changed site.

Parameters

$new_siteWP_Siterequired
The site object that has been inserted, updated or deleted.
$old_siteWP_Site|nulloptional
If $new_site has been updated, this must be the previous state of that site.

Default:null

Source

function wp_maybe_update_network_site_counts_on_update( $new_site, $old_site = null ) {
	if ( null === $old_site ) {
		wp_maybe_update_network_site_counts( $new_site->network_id );
		return;
	}

	if ( $new_site->network_id !== $old_site->network_id ) {
		wp_maybe_update_network_site_counts( $new_site->network_id );
		wp_maybe_update_network_site_counts( $old_site->network_id );
	}
}

Changelog

Version Description
5.1.0 Introduced.