![]()
A WordPress Multisite network can contain hundreds or thousands of subsites, and a quick query may return only its default batch, leaving later sites out of your results. This guide shows how to build a complete WordPress Multisite Site List by setting the network scope explicitly, retrieving sites in predictable batches, and formatting and safely displaying the data.
You will also choose appropriate permissions and site-status filters, resolve each site URL through WordPress, and avoid common pagination and network-scope mistakes. The right rules depend on whether you need a private administrative inventory or a public directory: a public page should not expose private or inactive sites.
- PHP 8.0 or later and a WordPress installation with Multisite enabled.
- Network administrator access, or a development environment where you can add and test the code.
- A current backup and, preferably, a staging environment before changing plugin or theme code.
- Basic familiarity with PHP arrays and WordPress functions, plus a small custom plugin or must-use plugin where you can place the implementation.
Step 1: Confirm the Network and Access You Need
First, confirm that the code runs on a WordPress Multisite installation. The get_sites() function retrieves sites from a network; a standalone WordPress site has no Multisite network inventory to query. Decide whether you need the network currently running the code or a different, specifically selected network.
For a private administrative inventory, require network-management access before returning any site data. Do not assume that a public shortcode should expose every site: public directories need separate visibility rules and should include only sites intended for public display.
function pnet_get_authorized_inventory_network_id( $network_id = null ) {
if ( ! is_multisite() || ! current_user_can( 'manage_network' ) ) {
return 0;
}
if ( null === $network_id || '' === $network_id ) {
return (int) get_current_network_id();
}
$network_id = absint( $network_id );
if ( 0 === $network_id || ! get_network( $network_id ) ) {
return 0;
}
return $network_id;
}
Call the helper without an argument to use get_current_network_id(). Pass an explicit network ID only when the task requires another network; the helper checks that the value is a valid network, and the capability check prevents unauthorized inventory access. A site ID or blog ID is not a network ID, so do not substitute one or trust an unvalidated request value.
Use this guard before retrieving administrative results, then pass its nonzero return value as the query network scope. Keep custom PHP functions prefixed with pnet_ to reduce the chance of naming collisions with WordPress or other plugins.
Did you know: You can explore our complete guide on Enable SVG in WordPress for step-by-step details.
Step 2: Fetch Sites in Predictable Batches
Set network_id to the authorized network ID from Step 1. The number argument limits each request, offset selects the next batch, and orderby plus order keep the results in stable site-ID order. Use fields set to all when the next step needs WP_Site objects. Relying on the default number can silently omit sites on a larger network.
This helper requests 100 sites at a time and advances by the number actually returned. Its status arguments are deliberate: the administrative inventory includes all statuses, while setting $public_only excludes non-public, archived, mature, spam, and deleted sites. Batches limit the number of records held per query, though the completed array still occupies memory.
function pnet_get_sites_in_batches( $network_id, $public_only = false ) {
$network_id = pnet_get_authorized_inventory_network_id( $network_id );
if ( 0 === $network_id ) {
return array();
}
$batch_size = 100;
$offset = 0;
$sites = array();
do {
$batch = get_sites(
array(
'network_id' => $network_id,
'number' => $batch_size,
'offset' => $offset,
'orderby' => 'id',
'order' => 'ASC',
'fields' => 'all',
'public' => $public_only ? 1 : null,
'archived' => $public_only ? 0 : null,
'mature' => $public_only ? 0 : null,
'spam' => $public_only ? 0 : null,
'deleted' => $public_only ? 0 : null,
)
);
$count = count( $batch );
if ( 0 === $count ) {
break;
}
$sites = array_merge( $sites, $batch );
$offset += $count;
} while ( $count === $batch_size );
return $sites;
}
For an administrative inventory, call the helper with false; use true only for a public directory with those visibility rules. The next step will shape each returned object into the fields your display needs.

Inventory Changes
Did you know: You can explore our complete guide on UpdraftPlus Premium Review for step-by-step details.
Step 3: Shape Each Site into Useful Data
A WP_Site object contains more information than most templates or API consumers need. Convert each object into a small, predictable array with an integer site ID, its domain and path, and the front-end URL. Returning only the fields your application uses makes the data easier to work with and avoids exposing unnecessary internal properties.
Use get_home_url() for the site’s front-end address. get_site_url() instead returns the URL for the site’s WordPress application, which can differ when WordPress is installed in a subdirectory. Let WordPress resolve URLs rather than joining the domain and path yourself; this matters for mapped domains, subdirectory paths, and subdomain networks.
function pnet_shape_sites( $sites ) {
$items = array();
foreach ( $sites as $site ) {
if ( ! ( $site instanceof WP_Site ) ) {
continue;
}
$site_id = (int) $site->blog_id;
$items[] = array(
'blog_id' => $site_id,
'domain' => (string) $site->domain,
'path' => (string) $site->path,
'home_url' => get_home_url( $site_id ),
);
}
return $items;
}
Pass the sites returned by the batch helper into pnet_shape_sites(). The resulting values are ready for a template or another application layer; escape them when rendering HTML. Test the resolved URLs against mapped-domain, subdirectory, and subdomain sites. This function does not switch the active blog, so later code continues in its original site context.

Step 4: Display the Inventory with a Shortcode or Template
Register a shortcode so network administrators can place the inventory on a private page. This callback reuses the batch retrieval and shaping helpers from the earlier steps, checks access before querying, and escapes every value at the HTML output boundary.
function pnet_render_site_inventory_shortcode() {
if ( ! is_multisite() || ! current_user_can( 'manage_network' ) ) {
return '<p>You are not authorized to view this network inventory.</p>';
}
$network_id = pnet_get_authorized_inventory_network_id();
if ( 0 === $network_id ) {
return '<p>The network inventory is unavailable.</p>';
}
$site_objects = pnet_get_sites_in_batches( $network_id, false );
$items = pnet_shape_sites( $site_objects );
if ( empty( $items ) ) {
return '<p>No sites were found for this network.</p>';
}
$html = '<ul class="pnet-site-inventory">';
foreach ( $items as $item ) {
$label = $item['domain'] . $item['path'];
$html .= '<li><a href="' . esc_url( $item['home_url'] ) . '">';
$html .= esc_html( $label );
$html .= '</a> <span>(Site ID: ' . esc_html( (string) $item['blog_id'] ) . ')</span></li>';
}
$html .= '</ul>';
return $html;
}
add_shortcode( 'pnet_site_inventory', 'pnet_render_site_inventory_shortcode' );
Add [pnet_site_inventory] to a page visible only to network administrators. If a theme template is a better fit, call pnet_get_sites_in_batches() and pnet_shape_sites() there, then apply the same escaping to each value. This implementation is intentionally administrative; a public directory needs a separate retrieval policy that includes only sites deliberately approved for public listing, excluding private, archived, spam, and deleted sites.
Access Control
You might also like:
Step 5: Test the Results and Plan for Scale
Validate the helper on staging before deploying it. Start with a network containing fewer than 100 sites, then test one with more than the configured batch size. Confirm that later batches appear, every site ID occurs once, and the final short batch is included. A single successful query is not enough to prove that pagination works.
Check that the default call lists only the current network, and that any explicitly selected network returns only its own sites. Include a public site, a private or archived site, and a site using a mapped domain. Verify that your chosen status filters match the intended inventory and that each displayed URL resolves to the expected front end.
For a very large network, paginate the rendered interface so visitors do not receive one enormous HTML response. Fetching database rows in batches limits each query, but the current helper still accumulates the complete result array in memory. If the interface needs only one page of results, design retrieval and rendering around that page rather than building the full array first.
Caching can reduce repeated work when occasional staleness is acceptable. Use a short-lived transient or an appropriate object cache, and invalidate or refresh it when sites are created, deleted, or change status if the inventory must stay current. Review the official get_sites() reference for the query arguments supported by your WordPress version.
You might also like:
Common Errors When Listing Multisite Sites
An incomplete or misleading inventory usually comes from query limits, incorrect network scope, status filters, or URL handling. Check these areas before assuming a site is missing or has been deleted.
The Query Returns Only the First Batch
Set both number and offset in the get_sites() query. In the retrieval loop, increase the offset by the number of sites actually returned and continue until the final batch is shorter than the batch size or empty. One unpaginated call does not confirm that the network has no more sites.
The List Contains Sites from the Wrong Network
Check that network_id is a network ID, not a site or blog ID. Use get_current_network_id() for the active network. If a request supplies another ID, validate it and confirm the current user is authorized to view that network before querying.
Private or Inactive Sites Are Missing or Exposed
Review the public, archived, mature, spam, and deleted arguments. An administrative inventory and a public directory need different rules. Restrict administrative output by capability, and do not expose private or inactive site details merely by hiding their links.
URLs Are Incorrect or the Wrong Site Context Persists
Use get_home_url() for front-end addresses, especially with mapped domains or subdirectory networks, and escape printed links with esc_url(). If code switches blogs to read site-specific data, pair each switch_to_blog() call with restore_current_blog().
Troubleshooting
The Query Returns Only the First 100 Sites
A plain get_sites() call can return only the first 100 results because the query has a default limit. That does not mean the network contains only 100 sites. Set number to the batch size you intend to retrieve and set offset to the position where each next batch should begin. For example, with batches of 100, request offsets 0, 100, 200, and so on.
Check the loop that assembles the results: after each request, it should advance the offset by the actual number of sites returned, not by an unrelated value. Continue while a full batch is returned; a shorter batch marks the end, and an empty batch confirms there are no more results. Advancing by the actual count also avoids making assumptions about how many records the query returned.
When diagnosing a short list, inspect the effective number and offset values, along with the network and status filters. A single unpaginated query is not proof that later sites are absent; compare the total against the number of results gathered across all batches.
The List Contains Sites from the Wrong Network
A list from an unexpected group of sites usually points to the scope value, not a failure in get_sites(). The network_id argument expects a network ID; it does not accept a site ID, also called a blog ID. These identifiers can both be integers, so a valid-looking number may still select the wrong network. Compare the query argument with the intended network record rather than inferring its meaning from the number alone.
When the inventory should represent the network where the code is running, obtain the scope with get_current_network_id() and pass that value explicitly to every batch request. Do not substitute get_current_blog_id(): that identifies the current site, not its network. Keep the chosen ID unchanged across offsets so later batches cannot silently query a different scope.
For an alternate network, validate any submitted identifier as a positive integer, confirm get_network() returns a network, and authorize the current user for that inventory before querying. Never accept a request parameter just because it is numeric; it could expose site names, domains, and URLs from a network the visitor should not inspect. If your application intentionally supports several networks, keep the permitted network IDs explicit and apply the access check to the selected one.
Private or Inactive Sites Are Missing or Exposed
Check the public, archived, mature, spam, and deleted arguments passed to get_sites(). These flags determine which site records the query returns; they do not determine who is allowed to view the results. An administrative inventory may intentionally include sites in every status, while a public directory should normally request only sites marked public and exclude archived, mature, spam, and deleted sites.
Keep those purposes separate. For an administrative inventory, check current_user_can( 'manage_network' ) before retrieving or rendering site details, including names, domains, and URLs. For a public-facing list, restrict the query to sites deliberately approved for public display and return only the information the directory needs. A public status flag is not a substitute for access control, and hiding a link or markup does not protect data that the page still outputs.
If a site appears missing, compare the filters with the intended inventory policy before changing them. If internal details appear on a public page, tighten both the query and the output rather than relying on visitors not finding the page.
URLs Are Incorrect or the Wrong Site Context Persists
A site URL can be wrong when code builds it by joining a domain and path. That approach can break with mapped domains, subdirectory paths, or subdomain networks. Use get_home_url( $site_id ) for the front-end address. Use get_site_url( $site_id ) only when you specifically need the site’s WordPress application URL; it can differ from the front-end URL.
The site-shaping helper should resolve the URL for each site ID directly, without switching the active blog. When printing the result, pass it through esc_url(). If you do switch blogs to read per-site options, always restore the original context—even if the work throws an error:
$switched = switch_to_blog( $site_id );
try {
$site_name = get_option( 'blogname' );
} finally {
if ( $switched ) {
restore_current_blog();
}
}
The finally block ensures restoration happens after successful work or an exception. Checking the return value avoids restoring when the requested site was already active and no switch occurred. A lingering switched context can make later URL or option lookups use the wrong site.
Test the resulting links on a mapped-domain site and a subdirectory site, then confirm that the expected front page opens. Escape the final URL at output time, even when WordPress generated it.
Conclusion: Build a Complete and Safe Network Inventory
An accurate network inventory starts by confirming Multisite is active and that the caller is authorized for the intended network. Pass an explicit network ID to get_sites(), retrieve results in bounded batches with a correctly advancing offset, and choose public, archived, mature, spam, and deleted filters deliberately. This avoids treating one default-limited query as the complete network and prevents an administrative inventory from being confused with a public directory.
After retrieval, shape each WP_Site into only the values the application needs, resolve front-end addresses with get_home_url(), and escape URLs and labels when rendering. Keep administrative output behind a network capability check; public listings should include only sites approved for public display. Batching bounds each query, but an all-sites array and one enormous response can still consume memory, so paginate the interface as the network grows. Before deployment, test on staging with more sites than one batch, verify that IDs are unique and belong to the selected network, check status visibility and mapped-domain links, then confirm private data is not exposed. With explicit scope, deliberate visibility rules, and escaped output, the WordPress Multisite Site List is complete and safe to use.