钩子文档

networks_pre_query

💡 云策文档标注

概述

networks_pre_query 是一个 WordPress 过滤器,用于在网络查询执行前过滤网络数据。开发者可以通过此过滤器返回特定值来绕过 WordPress 的默认网络查询,从而自定义查询行为。

关键要点

  • 过滤器允许返回非空值以绕过默认查询,返回值类型取决于查询变量:当 $this->query_vars['count'] 设置时返回整数计数;当 'ids' === $this->query_vars['fields'] 时返回网络 ID 数组;否则返回 WP_Network 对象数组。
  • 如果返回网络数据数组,它将被赋值给当前 WP_Network_Query 实例的 networks 属性。
  • 建议需要分页信息的过滤函数设置 WP_Network_Query 对象的 found_networks 和 max_num_pages 属性,该对象通过引用传递给过滤器。

注意事项

  • 如果 WP_Network_Query 不执行数据库查询,它可能无法自行生成分页信息,因此开发者应手动设置相关属性。

📄 原文内容

Filters the network data before the query takes place.

Description

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.

Parameters

$network_dataarray|int|null
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.
$queryWP_Network_Query
The WP_Network_Query instance, passed by reference.

Source

$network_data = apply_filters_ref_array( 'networks_pre_query', array( $network_data, &$this ) );

Changelog

Version Description
5.6.0 The returned array of network data is assigned to the networks property of the current WP_Network_Query instance.
5.2.0 Introduced.