[wp-trac] [WordPress Trac] #54868: [WP_User_Query][Multisite] Run queries in the context of the correct site / blog_id

WordPress Trac noreply at wordpress.org
Thu Jan 20 21:23:57 UTC 2022

#54868: [WP_User_Query][Multisite] Run queries in the context of the correct site /
 Reporter:  janthiel                |      Owner:  (none)
     Type:  defect (bug)            |     Status:  new
 Priority:  normal                  |  Milestone:  Awaiting Review
Component:  General                 |    Version:
 Severity:  normal                  |   Keywords:
  Focuses:  multisite, performance  |
 Context: WP Multisite

 `WP_User_Query` can be executed from any site to query for users from any
 other site. This is done using the `blog_id` parameter.

 $user_query = new WP_User_Query( array( 'blog_id' => 42 ) );
 Contrary to the commonly used pattern when data is fetched from a specific
 site within a network, WP_User_Query does __not__ make use of the
 `switch_to_blog` / `restore_current_blog` functions and thus executes code
 always within the context of the site which called the query instead of
 the site the query was actually called for.

 This has some implications:
 1. Low level functions called within WP_Query are **not** aware of the
 correct context. Especially the `WP Object Cache` called from
 `cache_users` is not aware of the correct site and thus stores the
 resulting data at the ''wrong'' site. This has no impact running default
 WP installations but is conceptionally wrong and prevents any flexibility.
 2. Superfluous code could be removed
                 if ( 'all_with_meta' === $qv['fields'] ) {
                         cache_users( $this->results );

                         $r = array();
                         foreach ( $this->results as $userid ) {
                                 $r[ $userid ] = new WP_User( $userid, '',
 $qv['blog_id'] );

                         $this->results = $r;
                 } elseif ( 'all' === $qv['fields'] ) {
                         foreach ( $this->results as $key => $user ) {
                                 $this->results[ $key ] = new WP_User(
 $user, '', $qv['blog_id'] );
 Just one example: The calls to `new WP_User` could be reduced to `new
 WP_User( $user )` if done within the correct site context already. All the
 `wp_cache_<op>` calls within `WP_User` will also run within the ''wrong''
 site context.

 Using `switch_to_blog` shifts all `wp_cache_<op>` and all other functions,
 hooks, etc. into the right context. The global `blog_id` will be set
 correctly to the site that is queried instead of the site that is
 querying. This will also follow standard WordPress Multisite patterns to
 access data from other sites.

Ticket URL: <https://core.trac.wordpress.org/ticket/54868>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform

More information about the wp-trac mailing list