[wp-trac] [WordPress Trac] #37958: Improve looping through sites and restoring

WordPress Trac noreply at wordpress.org
Tue Sep 6 18:13:00 UTC 2016


#37958: Improve looping through sites and restoring
--------------------------------+-------------------------------------
 Reporter:  tfrommen            |       Owner:
     Type:  enhancement         |      Status:  new
 Priority:  normal              |   Milestone:  Awaiting Review
Component:  Networks and Sites  |     Version:
 Severity:  normal              |  Resolution:
 Keywords:                      |     Focuses:  multisite, performance
--------------------------------+-------------------------------------

Comment (by johnjamesjacoby):

 Neat idea. And IIRC, this is actually how `switch_to_blog()` originally
 worked in the early days, around WordPress 2.5. The adverse effect for the
 rest of core was noticed in places where switching happened on actions &
 filters, where it wouldn't/couldn't properly clean up after itself,
 leaving the environment in an unpredictable state.

 I've always felt this type of approach is most useful when you already
 know ahead of time an array of site ID's you want to quickly switch
 between. If we can count on that always being true, maybe the way to
 implement this is actually on-top of the existing API with a `$quick`
 parameter, and a `stash_current_blog()` helper that gets called before the
 loop happens.

 {{{
 // Fetch blog IDs
 $blogs = get_sites( $whatever );

 // Stash
 stash_current_blog();

 // Loop
 foreach ( $blogs as $blog ) {
     switch_to_blog( $blog->blog_id );
 }

 // Restore
 restore_current_blog();
 }}}

--
Ticket URL: <https://core.trac.wordpress.org/ticket/37958#comment:2>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list