WP_Network_Query::get_networks(): array|int
- Since
- 4.6.0
- Source
wp-includes/class-wp-network-query.php:198
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
- Scaling
- Scales with input
- Instructions
- 21–121
- Plugin surface
- 3 hooks
- Called by
- 1
Only touches the object cache, via wp_cache_get_last_changed().
The body loops, so the work grows with how much data it finds.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 147.
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.
1 place in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
do_action_ref_array()called directly - cacheobject cache
wp_cache_get_last_changed()called directly - serializeserialisation
serialize()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.
| When | Instructions | Calls it makes |
|---|---|---|
$network_data !== null | 21–26 | ->parse_query() |
$network_data === null && $cache_value !== false | 62–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 !== false | 73–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 === false | 73–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 !== false | 75 | ->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 === false | 75–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 === false | 84–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 !== false | 86 | ->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 === false | 86 | ->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 === false | 86–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 === false | 88 | ->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 !== false | 91–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 !== false | 94–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 === false | 97 | ->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 === false | 99 | ->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 === false | 102–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 !== false | 104–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 === false | 104–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 === false | 105–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 !== false | 107–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 === false | 107–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 === false | 115–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 === false | 117–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 === false | 118–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 === false | 120–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:
- apply_filters( networks_pre_query )filterline 242 (+44 into the body)
Filters the network data before the query takes place.
Uses · 11
- do_action_ref_array()Calls the callback functions that have been added to an action hook, specifying arguments in an array.
- apply_filters_ref_array()Calls the callback functions that have been added to a filter hook, specifying arguments in an array.
- wp_array_slice_assoc()Extracts a slice of an array, given a list of keys.
- wp_cache_get_last_changed()Gets last changed date for the specified cache group.
- wp_cache_get_salted()Retrieves cached data if valid and unchanged.
- wp_cache_set_salted()Stores salted data in the cache.
- _prime_network_caches()Adds any networks from the given IDs to the cache that do not already exist in cache.
- get_network()Retrieves network data given a network ID or network object.
- WP_Network_Query::parse_query()Parses arguments passed to the network query with default query parameters.
- WP_Network_Query::get_network_ids()Used internally to get a list of network IDs matching the query vars.
- WP_Network_Query::set_found_networks()Populates found_networks and max_num_pages properties for the current query if the limit clause was used.
Used by · 1
- WP_Network_Query::query()Sets up the WordPress query for retrieving networks.
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.
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.