Make WordPress Core


Ignore:
Timestamp:
08/10/2023 10:24:29 AM (15 months ago)
Author:
SergeyBiryukov
Message:

Users: Correct the logic for switch_to_blog() in WP_User_Query::generate_cache_key().

If $blog_id equals 0, it should be treated as the current site ID, and there is no need to switch to a different site.

This commit prevents an unnecessary call to switch_to_blog() on single site to avoid a fatal error when using 'orderby' => 'post_count' and the deprecated 'who' => 'authors' parameter:

Uncaught Error: Call to undefined function switch_to_blog() in wp-includes/class-wp-user-query.php:1077

Follow-up to [55657].

Props dd32, austinginder, RavanH, mukesh27.
Fixes #59011.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-user-query.php

    r56353 r56381  
    10721072            $blog_id = absint( $args['blog_id'] );
    10731073        }
    1074         if ( ( $args['has_published_posts'] && $blog_id ) || in_array( 'post_count', $ordersby, true ) ) {
    1075             $switch = get_current_blog_id() !== $blog_id;
     1074
     1075        if ( $args['has_published_posts'] || in_array( 'post_count', $ordersby, true ) ) {
     1076            $switch = $blog_id && get_current_blog_id() !== $blog_id;
    10761077            if ( $switch ) {
    10771078                switch_to_blog( $blog_id );
    10781079            }
     1080
    10791081            $last_changed .= wp_cache_get_last_changed( 'posts' );
     1082
    10801083            if ( $switch ) {
    10811084                restore_current_blog();
Note: See TracChangeset for help on using the changeset viewer.