[wp-trac] [WordPress Trac] #60290: Changeset #56635 breaks template loading in multisite network using switch_to_blog()

WordPress Trac noreply at wordpress.org
Fri Jan 19 14:31:05 UTC 2024


#60290: Changeset #56635 breaks template loading in multisite network using
switch_to_blog()
-----------------------------+------------------------
 Reporter:  metropolis_john  |       Owner:  joemcgill
     Type:  defect (bug)     |      Status:  reviewing
 Priority:  normal           |   Milestone:  6.5
Component:  Themes           |     Version:  6.4.2
 Severity:  normal           |  Resolution:
 Keywords:  needs-patch      |     Focuses:
-----------------------------+------------------------

Comment (by metropolis_john):

 @joemcgill In this particular case we have a multisite network where the
 "main" site is a portal where users can login and maintain a portfolio of
 uploaded artwork. There are 2 other sites in the network that both query
 artists (users) and artwork from the main site using `switch_to_blog()`.
 The two sites have unique themes and each use unique template parts to
 display an artist or piece of artwork.

 As we loop through the queried users we sometimes need to make an
 additional query to check if they have authored any posts (artwork) with
 specific parameters, so in those cases rather than bouncing back and forth
 between sites it makes sense to use `switch_to_blog()` only once and
 `restore_current_blog()` after the loop, while the loop contains a
 `get_template_part()` call.

 That might look something like this:


 {{{
 switch_to_blog(1);

 $artists = new WP_User_Query( array(
   'role'     => 'artist',
   'meta_key' => 'last_name',
   'orderby'  => 'meta_value',
 ) );

 foreach( $artists->get_results() as $artist ) {

   $artwork = new WP_Query( array(
     'post_type'      => 'artwork',
     'posts_per_page' => 1,
     'author'         => $artist->ID,
     'meta_query'     => array(
       array(
         'key'        => 'price',
         'value'      => '',
         'compare'    => '!=',
       ),
     ),
   ) );

   if( $artwork->have_posts() ) {
     get_template_part( 'template-parts/artist' );
   }

 }

 restore_current_blog();
 }}}

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


More information about the wp-trac mailing list