get_sites()
云策文档标注
概述
get_sites() 函数用于检索匹配指定参数的站点列表,基于 WP_Site_Query 类实现。它返回 WP_Site 对象数组、站点 ID 数组或站点数量,具体取决于参数设置。
关键要点
- 函数接受可选参数 $args,可以是数组或字符串,用于指定查询条件,参考 WP_Site_Query::__construct()。
- 返回值类型可变:默认返回 WP_Site 对象数组;当 'fields' 设为 'ids' 时返回站点 ID 数组;当 'count' 作为查询变量时返回站点数量。
- 自 WordPress 4.6.0 引入,替代了已弃用的 wp_get_sites() 函数,注意返回类型从数组变为对象数组。
- 默认限制返回前 100 个站点,可通过 'number' 参数调整,设置为 0 可获取所有站点,但 -1 无效。
- 参数 'limit' 在 wp_get_sites() 中自动转换为 'number',但在 get_sites() 中需直接使用 'number'。
代码示例
// 获取所有站点(WordPress 4.6+)
if ( function_exists('get_sites') && class_exists('WP_Site_Query') ) {
$sites = get_sites();
foreach ( $sites as $site ) {
switch_to_blog( $site->blog_id );
// 执行操作
restore_current_blog();
}
}
// 使用参数限制返回站点数量
$args = array(
'number' => 420,
);
$blog_list = get_sites( $args );
// 将站点对象转换为数组以访问属性
$subsites = get_sites();
foreach ( $subsites as $subsite ) {
$subsite_id = get_object_vars($subsite)["blog_id"];
$subsite_name = get_blog_details($subsite_id)->blogname;
echo 'Site ID/Name: ' . $subsite_id . ' / ' . $subsite_name . 'n';
}注意事项
- 从 wp_get_sites() 迁移时,注意返回类型差异:get_sites() 返回对象数组,而非多维数组,循环时需使用对象属性(如 $site->blog_id)或 get_object_vars() 转换。
- 参数 'number' 默认值为 100,若不指定可能只影响前 100 个站点,需根据需求调整。
- 函数在 WordPress 4.8.0 中新增了 'lang_id'、'lang__in' 和 'lang__not_in' 参数,用于语言过滤。
原文内容
Retrieves a list of sites matching requested arguments.
Description
See also
Parameters
$argsstring|arrayoptional-
Array or string of arguments. See WP_Site_Query::__construct() for information on accepted arguments.
Default:
array()
Source
function get_sites( $args = array() ) {
$query = new WP_Site_Query();
return $query->query( $args );
}
Skip to note 5 content
Lance Cleveland
Also good to know… get_sites now returns an OBJECT not a named array.
A code example that may help:
// WordPress 4.6 // if ( function_exists( 'get_sites' ) && class_exists( 'WP_Site_Query' ) ) { $sites = get_sites(); foreach ( $sites as $site ) { switch_to_blog( $site->blog_id ); // do something restore_current_blog(); } return; } // WordPress < 4.6 // if ( function_exists( 'wp_get_sites' ) ) { $sites = wp_get_sites(); foreach ( $sites as $site ) { switch_to_blog( $site['blog_id'] ); // do something restore_current_blog(); } return; }Skip to note 6 content
mkormendy
Beware, using
get_sites()as a drop-in forwp_get_sites()may not produce results as expected.PHP Fatal error: Cannot use object of type WP_Site as array in /path/to/code/that/uses/get_sites/method/file.phpIt’s true that
get_sites()returns an array, however, it produces an array of sites as objects. This is different fromwp_get_sites(), which used to produce a multidimensional array of the sites, with their properties in a secondary array dimension (simply an array of site arrays with that site’s properties).If you’re attempting to loop through the sites to get the properties of each site with get_sites() , you’ll need to convert each site object to an array using
get_object_vars( object )http://www.php.net/manual/en/function.get-object-vars.php.See the example below noting the use of get_object_vars on line three:
$subsites = get_sites(); foreach( $subsites as $subsite ) { $subsite_id = get_object_vars($subsite)["blog_id"]; $subsite_name = get_blog_details($subsite_id)->blogname; echo 'Site ID/Name: ' . $subsite_id . ' / ' . $subsite_name . 'n'; }This should return the following list of sites:
Site ID/Name: 1 / SiteNameOne
Site ID/Name: 2 / SiteNameTwo
Site ID/Name: 3 / SiteNameThree
Skip to note 7 content
Lance Cleveland
wp_get_sites() ‘limit’ argument is now ‘number’.
wp_get_sites() converted this to $args[‘number’] for you, get_sites() does not appear to handle this parameter name conversion for you.
Reference: https://developer.wordpress.org/reference/functions/wp_get_sites/
Skip to note 8 content
Clicknathan
The number of sites returned is limited to 100 by default. you can increase this using
number.$args = array( 'number' => 420, ); $blog_list = get_sites( $args );Note that -1 (often used to “get all”) does not appear to work here.