wppaste
WordPress

WP_Network_Query::get_networks(): array|int

Since
4.6.0
Source
wp-includes/class-wp-network-query.php:198
Gets a list of networks matching the query vars.

Compatibility

WordPress
since 4.6.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Return value

array|int
List of WP_Network objects, a list of network IDs when 'fields' is set to 'ids', or the number of networks when 'count' is passed as a query var.

Performance profile

How much work a call to WP_Network_Query::get_networks() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Moderate

Only touches the object cache, via wp_cache_get_last_changed().

Scaling
Scales with input

The body loops, so the work grows with how much data it finds.

Instructions
21–121

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 147.

Plugin surface
3 hooks

Third-party callbacks on 'pre_get_networks', 'networks_pre_query', 'the_networks' run inside this call, and their cost is not bounded by anything here.

Called by
1

1 place in core call this, so the cost is paid more often than your own code shows.

What it touches

  • hookthird-party callbacksdo_action_ref_array()called directly
  • cacheobject cachewp_cache_get_last_changed()called directly
  • serializeserialisationserialize()called directly

What one call costs · 25 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_Network_Query::get_networks() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
$network_data !== null21–26->parse_query()
$network_data === null && $cache_value !== false62–65->parse_query(), array_keys(), wp_array_slice_assoc(), serialize(), md5(), wp_cache_get_last_changed(), wp_cache_get_salted()
$network_data === null && $cache_value !== false73–76->parse_query(), array_keys(), wp_array_slice_assoc(), serialize(), md5(), wp_cache_get_last_changed(), wp_cache_get_salted(), array_map()
$network_data === null && $cache_value === false73–76->parse_query(), array_keys(), wp_array_slice_assoc(), serialize(), md5(), wp_cache_get_last_changed(), wp_cache_get_salted(), ->get_network_ids(), wp_cache_set_salted()
$network_data === null && $cache_value !== false75->parse_query(), array_keys(), wp_array_slice_assoc(), serialize(), md5(), wp_cache_get_last_changed(), wp_cache_get_salted(), ceil()
$network_data === null && $cache_value === false75–78->parse_query(), array_keys(), wp_array_slice_assoc(), serialize(), md5(), wp_cache_get_last_changed(), wp_cache_get_salted(), ->get_network_ids(), ->set_found_networks(), wp_cache_set_salted()
$network_data === null && $cache_value === false84–87->parse_query(), array_keys(), wp_array_slice_assoc(), serialize(), md5(), wp_cache_get_last_changed(), wp_cache_get_salted(), ->get_network_ids(), wp_cache_set_salted(), array_map()
$network_data === null && $cache_value !== false86->parse_query(), array_keys(), wp_array_slice_assoc(), serialize(), md5(), wp_cache_get_last_changed(), wp_cache_get_salted(), ceil(), array_map()
$network_data === null && $cache_value === false86->parse_query(), array_keys(), wp_array_slice_assoc(), serialize(), md5(), wp_cache_get_last_changed(), wp_cache_get_salted(), ->get_network_ids(), wp_cache_set_salted(), ceil()
$network_data === null && $cache_value === false86–89->parse_query(), array_keys(), wp_array_slice_assoc(), serialize(), md5(), wp_cache_get_last_changed(), wp_cache_get_salted(), ->get_network_ids(), ->set_found_networks(), wp_cache_set_salted(), array_map()
$network_data === null && $cache_value === false88->parse_query(), array_keys(), wp_array_slice_assoc(), serialize(), md5(), wp_cache_get_last_changed(), wp_cache_get_salted(), ->get_network_ids(), ->set_found_networks(), wp_cache_set_salted(), ceil()
$network_data === null && $cache_value !== false91–95->parse_query(), array_keys(), wp_array_slice_assoc(), serialize(), md5(), wp_cache_get_last_changed(), wp_cache_get_salted(), array_map(), array_map()
13 further outcomes, up to 121 instructions
$network_data === null && $cache_value !== false94–98->parse_query(), array_keys(), wp_array_slice_assoc(), serialize(), md5(), wp_cache_get_last_changed(), wp_cache_get_salted(), array_map(), _prime_network_caches(), array_map()
$network_data === null && $cache_value === false97->parse_query(), array_keys(), wp_array_slice_assoc(), serialize(), md5(), wp_cache_get_last_changed(), wp_cache_get_salted(), ->get_network_ids(), wp_cache_set_salted(), ceil(), array_map()
$network_data === null && $cache_value === false99->parse_query(), array_keys(), wp_array_slice_assoc(), serialize(), md5(), wp_cache_get_last_changed(), wp_cache_get_salted(), ->get_network_ids(), ->set_found_networks(), wp_cache_set_salted(), ceil(), array_map()
$network_data === null && $cache_value === false102–106->parse_query(), array_keys(), wp_array_slice_assoc(), serialize(), md5(), wp_cache_get_last_changed(), wp_cache_get_salted(), ->get_network_ids(), wp_cache_set_salted(), array_map(), array_map()
$network_data === null && $cache_value !== false104–105->parse_query(), array_keys(), wp_array_slice_assoc(), serialize(), md5(), wp_cache_get_last_changed(), wp_cache_get_salted(), ceil(), array_map(), array_map()
$network_data === null && $cache_value === false104–108->parse_query(), array_keys(), wp_array_slice_assoc(), serialize(), md5(), wp_cache_get_last_changed(), wp_cache_get_salted(), ->get_network_ids(), ->set_found_networks(), wp_cache_set_salted(), array_map(), array_map()
$network_data === null && $cache_value === false105–109->parse_query(), array_keys(), wp_array_slice_assoc(), serialize(), md5(), wp_cache_get_last_changed(), wp_cache_get_salted(), ->get_network_ids(), wp_cache_set_salted(), array_map(), _prime_network_caches(), array_map()
$network_data === null && $cache_value !== false107–108->parse_query(), array_keys(), wp_array_slice_assoc(), serialize(), md5(), wp_cache_get_last_changed(), wp_cache_get_salted(), ceil(), array_map(), _prime_network_caches(), array_map()
$network_data === null && $cache_value === false107–111->parse_query(), array_keys(), wp_array_slice_assoc(), serialize(), md5(), wp_cache_get_last_changed(), wp_cache_get_salted(), ->get_network_ids(), ->set_found_networks(), wp_cache_set_salted(), array_map(), _prime_network_caches(), array_map()
$network_data === null && $cache_value === false115–116->parse_query(), array_keys(), wp_array_slice_assoc(), serialize(), md5(), wp_cache_get_last_changed(), wp_cache_get_salted(), ->get_network_ids(), wp_cache_set_salted(), ceil(), array_map(), array_map()
$network_data === null && $cache_value === false117–118->parse_query(), array_keys(), wp_array_slice_assoc(), serialize(), md5(), wp_cache_get_last_changed(), wp_cache_get_salted(), ->get_network_ids(), ->set_found_networks(), wp_cache_set_salted(), ceil(), array_map(), array_map()
$network_data === null && $cache_value === false118–119->parse_query(), array_keys(), wp_array_slice_assoc(), serialize(), md5(), wp_cache_get_last_changed(), wp_cache_get_salted(), ->get_network_ids(), wp_cache_set_salted(), ceil(), array_map(), _prime_network_caches(), array_map()
$network_data === null && $cache_value === false120–121->parse_query(), array_keys(), wp_array_slice_assoc(), serialize(), md5(), wp_cache_get_last_changed(), wp_cache_get_salted(), ->get_network_ids(), ->set_found_networks(), wp_cache_set_salted(), ceil(), array_map(), _prime_network_caches(), array_map()

Across PHP versions

Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 147 instructions, 21–121 executed per call, 13 branches. The work does not change between versions.

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Hooks and filters fired · 3

3 hooks fire while WP_Network_Query::get_networks() runs, in this order:

  1. do_action( pre_get_networks )actionline 208 (+10 into the body)

    Fires before networks are retrieved.

  2. apply_filters( networks_pre_query )filterline 242 (+44 into the body)

    Filters the network data before the query takes place.

  3. apply_filters( the_networks )filterline 318 (+120 into the body)

    Filters the network query results.

Uses · 11

Used by · 1

Source code

	public function get_networks() {		$this->parse_query(); 		/**		 * Fires before networks are retrieved.		 *		 * @since 4.6.0		 *		 * @param WP_Network_Query $query Current instance of WP_Network_Query (passed by reference).		 */		do_action_ref_array( 'pre_get_networks', array( &$this ) ); 		$network_data = null; 		/**		 * Filters the network data before the query takes place.		 *		 * Return a non-null value to bypass WordPress' default network queries.		 *		 * The expected return type from this filter depends on the value passed		 * in the request query vars:		 * - When `$this->query_vars['count']` is set, the filter should return		 *   the network count as an integer.		 * - When `'ids' === $this->query_vars['fields']`, the filter should return		 *   an array of network IDs.		 * - Otherwise the filter should return an array of WP_Network objects.		 *		 * Note that if the filter returns an array of network data, it will be assigned		 * to the `networks` property of the current WP_Network_Query instance.		 *		 * Filtering functions that require pagination information are encouraged to set		 * the `found_networks` and `max_num_pages` properties of the WP_Network_Query object,		 * passed to the filter by reference. If WP_Network_Query does not perform a database		 * query, it will not have enough information to generate these values itself.		 *		 * @since 5.2.0		 * @since 5.6.0 The returned array of network data is assigned to the `networks` property		 *              of the current WP_Network_Query instance.		 *		 * @param array|int|null   $network_data Return an array of network data to short-circuit WP's network query,		 *                                       the network count as an integer if `$this->query_vars['count']` is set,		 *                                       or null to allow WP to run its normal queries.		 * @param WP_Network_Query $query        The WP_Network_Query instance, passed by reference.		 */		$network_data = apply_filters_ref_array( 'networks_pre_query', array( $network_data, &$this ) ); 		if ( null !== $network_data ) {			if ( is_array( $network_data ) && ! $this->query_vars['count'] ) {				$this->networks = $network_data;			} 			return $network_data;		} 		// $args can include anything. Only use the args defined in the query_var_defaults to compute the key.		$_args = wp_array_slice_assoc( $this->query_vars, array_keys( $this->query_var_defaults ) ); 		// Ignore the $fields, $update_network_cache arguments as the queried result will be the same regardless.		unset( $_args['fields'], $_args['update_network_cache'] ); 		$key          = md5( serialize( $_args ) );		$last_changed = wp_cache_get_last_changed( 'networks' ); 		$cache_key   = "get_network_ids:$key";		$cache_value = wp_cache_get_salted( $cache_key, 'network-queries', $last_changed ); 		if ( false === $cache_value ) {			$network_ids = $this->get_network_ids();			if ( $network_ids ) {				$this->set_found_networks();			} 			$cache_value = array(				'network_ids'    => $network_ids,				'found_networks' => $this->found_networks,			);			wp_cache_set_salted( $cache_key, $cache_value, 'network-queries', $last_changed );		} else {			$network_ids          = $cache_value['network_ids'];			$this->found_networks = $cache_value['found_networks'];

Changelog

Introduced in 4.6.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 7.1.0 tag, from src/wp-includes/class-wp-network-query.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.