4

I have a loop that pulls in all sites in my multisite site network and loops through them to get variables located in their ACF options. Here is an excerpt from the code I am using:

    $stageurl = array();
    $args = array(
        'public'     => true,
        'limit'      => 500
    );

    $sites = wp_get_sites($args);

    foreach ($sites as $site) {
        switch_to_blog($site['blog_id']);
        $stage = get_field('stage', 'option');
        if (isset($stage)) {
            $stageurl[] = $site['domain'];
        }
        restore_current_blog();
    }

    foreach ($stageurl as $i => $stages) {
        ...
    }

Using wp_get_sites, how are sites sorted by default?

Ideally I would like to sort sites by their creation date before adding them to my foreach loop. Is it possible to find a network site's creation date and use it to sort my $stageurl array?

2

Using get_sites() in WP 4.6+

It looks like wp_get_sites() will be deprecated in WP 4.6.

The new replacement is:

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

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

Very similar to get_posts() and WP_Query.

It supports various useful parameters and filters.

Here's what the inline documentation says about the orderby input parameter:

  • Site status or array of statuses.
  • Default id
  • Accepts:

    • id
    • domain
    • path
    • network_id
    • last_updated
    • registered
    • domain_length
    • path_length
    • site__in
    • network__in
  • Also accepts the following to disable ORDER BY clause:

    • false
    • an empty array
    • none

The default of the order parameter is DESC.

Example

Here's an example (untested) how we might try to order public sites by registration date:

$mysites = get_sites( 
    [
        'public'  => 1,
        'number'  => 500,
        'orderby' => 'registered',
        'order'   => 'DESC',
    ]
);

and returning at max 500 sites.

Update

Thanks to @fostertime for noticing that the boolean value of the public parameter. It's not supported. It should be 1 not true in the example here above.

I therefore filed a ticket here (#37937) to support boolean strings for the public, archived, mature, spam and deleted attributes in WP_Site_Query.

6

@birgire get_sites() example is the correct way. I would have just added this as a comment, but don't have enough dang reputation!

The only change, is that public accepts an integer, not a bool. See https://developer.wordpress.org/reference/functions/get_sites/

$mysites = get_sites( 
    [
        'public'  => 1,
        'number'  => 500,
        'orderby' => 'id',
        'order'   => 'ASC',
    ]
);
  • Thanks for noticing that ;-) I filed a ticket to support boolean strings (see my updated answer). – birgire Sep 22 '16 at 13:58
3

I wasn't able to get get_sites working, so here is a solution using wp_get_sites until further documentation is released on the new version.

wp_get_sites returns a lot of data about the subsite, including creation and last modified dates. I turned my original $stageurl array into a multidimensional array and added registration date as a value. The default value returned was a string, so I converted it to time before calling it. Finally, I sorted it to show the newest sites first and oldest sites at the end of the list.

foreach ($sites as $site) {
    switch_to_blog($site['blog_id']);
    $stage = get_field('stage', 'option');
    $registered = strtotime($site['registered']);

    if (isset($stage)) {
        $stageurl[] = array('domain' => $site['domain'], 'registered' => $registered);
    }

    restore_current_blog();
}

usort($stageurl, function($a, $b) {
    return $b['registered'] - $a['registered'];
});
3

Here is my code for running through sites in WP 4.6 and sites using WP < 4.6. If you are producing themes or plugins to be used by the general public you have no control over which version of WordPress they are using.

The problem is not just the parameter changes between get_sites and wp_get_sites but also that WordPress 4.6 has changed the return value from an array to an object. Therefore you need to reference your elements differently.

Here I first check that the WP 4.6 function is available and use that with the object version first. I then move on to checking that pre 4.6 wp_get_sites and try doing that iteration next.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.