get_sites() - WordPress 函数参考中文文档

返回介绍

get_sites()

发布于 2017-09-11 字数8312 浏览 1011 评论 0

get_sites( string|array $args = array() )

Retrieves a list of sites matching requested arguments.


描述


参数

$args

(string|array)
(Optional)
Array or query string of site query parameters.

  • ‘site__in’
    (array) Array of site IDs to include.
  • ‘site__not_in’
    (array) Array of site IDs to exclude.
  • ‘count’
    (bool) Whether to return a site count (true) or array of site objects. Default false.
  • ‘date_query’
    (array) Date query clauses to limit sites by. See WP_Date_Query. Default null.
  • ‘fields’
    (string) Site fields to return. Accepts ‘ids’ (returns an array of site IDs) or empty (returns an array of complete site objects).
  • ‘ID’
    (int) A site ID to only return that site.
  • ‘number’
    (int) Maximum number of sites to retrieve. Default 100.
  • ‘offset’
    (int) Number of sites to offset the query. Used to build LIMIT clause. Default 0.
  • ‘no_found_rows’
    (bool) Whether to disable the SQL_CALC_FOUND_ROWS query. Default true.
  • ‘orderby’
    (string|array) Site status or array of statuses. Accepts ‘id’, ‘domain’, ‘path’, ‘network_id’, ‘last_updated’, ‘registered’, ‘domain_length’, ‘path_length’, ‘site__in’ and ‘network__in’. Also accepts false, an empty array, or ‘none’ to disable ORDER BY clause. Default ‘id’.
  • ‘order’
    (string) How to order retrieved sites. Accepts ‘ASC’, ‘DESC’. Default ‘ASC’.
  • ‘network_id’
    (int) Limit results to those affiliated with a given network ID. If 0, include all networks. Default 0.
  • ‘network__in’
    (array) Array of network IDs to include affiliated sites for.
  • ‘network__not_in’
    (array) Array of network IDs to exclude affiliated sites for.
  • ‘domain’
    (string) Limit results to those affiliated with a given domain.
  • ‘domain__in’
    (array) Array of domains to include affiliated sites for.
  • ‘domain__not_in’
    (array) Array of domains to exclude affiliated sites for.
  • ‘path’
    (string) Limit results to those affiliated with a given path.
  • ‘path__in’
    (array) Array of paths to include affiliated sites for.
  • ‘path__not_in’
    (array) Array of paths to exclude affiliated sites for.
  • ‘public’
    (int) Limit results to public sites. Accepts ‘1’ or ‘0’.
  • ‘archived’
    (int) Limit results to archived sites. Accepts ‘1’ or ‘0’.
  • ‘mature’
    (int) Limit results to mature sites. Accepts ‘1’ or ‘0’.
  • ‘spam’
    (int) Limit results to spam sites. Accepts ‘1’ or ‘0’.
  • ‘deleted’
    (int) Limit results to deleted sites. Accepts ‘1’ or ‘0’.
  • ‘lang_id’
    (int) Limit results to a language ID.
  • ‘lang__in’
    (array) Array of language IDs to include affiliated sites for.
  • ‘lang__not_in’
    (array) Array of language IDs to exclude affiliated sites for.
  • ‘search’
    (string) Search term(s) to retrieve matching sites for.
  • ‘search_columns’
    (array) Array of column names to be searched. Accepts ‘domain’ and ‘path’.
  • ‘update_site_cache’
    (bool) Whether to prime the cache for found sites. Default false.

Default value: array()


返回值

(array) List of sites.


源代码

File: wp-includes/ms-blogs.php

function get_sites( $args = array() ) {
	$query = new WP_Site_Query();

	return $query->query( $args );
}

更新日志

Version 描述
4.8.0 Introduced the ‘lang_id’, ‘lang<strong>in’, and ‘lang</strong>not_in’ parameters.
4.6.0 Introduced.

相关函数

Uses

  • wp-includes/class-wp-site-query.php:
    WP_Site_Query::__construct()

Used By

  • wp-admin/includes/class-wp-ms-sites-list-table.php:
    WP_MS_Sites_List_Table::prepare_items()
  • wp-admin/includes/class-wp-importer.php:
    WP_Importer::set_blog()
  • wp-includes/user.php:
    get_blogs_of_user()
  • wp-includes/ms-functions.php:
    wp_update_network_site_counts()
  • wp-includes/ms-deprecated.php:
    wp_get_sites()
  • wp-includes/ms-functions.php:
    domain_exists()
  • wp-includes/ms-functions.php:
    get_blog_id_from_url()
  • wp-includes/ms-load.php:
    get_site_by_path()
  • wp-includes/ms-blogs.php:
    get_id_from_blogname()
  • Show 4 more used by
    Hide more used by


User Contributed Notes

  1. Skip to note content

    You must log in to vote on the helpfulness of this noteVote results for this note: 2You must log in to vote on the helpfulness of this note

    Contributed by Store Locator Plus

    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;
    }	
    
  2. 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/

    Beware, using get_sites() as a drop-in for wp_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.php

    It’s true that get_sites() returns an array, however, it produces an array of sites as objects. This is different from wp_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

上一篇:get_site()

下一篇:get_sitestats()

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

目前还没有任何评论,快来抢沙发吧!