1

What is the easiest approach to iterate through the sub-sites (in Multi-Site network) and let's say add_option("xyz", 123) for each of site.

1

2 Answers 2

4

Well, you can get sites with get_sites and loop through them:

if ( function_exists( 'get_sites' ) && class_exists( 'WP_Site_Query' ) ) {
    $sites = get_sites();
    foreach ( $sites as $site ) {
        // do something
    }
    return;
}
3

I've found this so far to work for me:

foreach (get_sites() as $blog){
    switch_to_blog($blog->blog_id);
    // get_option (...)   
    restore_current_blog();
}
2
  • The only problem with this is that you use switch_to_blog which is very very bad for performance... And you don't need it to set option... Feb 5, 2019 at 8:31
  • @KrzysiekDróżdż forget the "get_option". what are alternatives for itteration then without switch_to_blog ? So loop could be used.
    – T.Todua
    Feb 6, 2019 at 18:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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