Opened 3 years ago
Closed 3 years ago
#59011 closed defect (bug) (fixed)
Fatal error on single-site get_users() under certain circumstances
| Reported by: | dd32 | Owned by: | SergeyBiryukov |
|---|---|---|---|
| Priority: | normal | Milestone: | 6.3.1 |
| Component: | Users | Version: | 6.3 |
| Severity: | normal | Keywords: | has-patch has-unit-tests fixed-major dev-reviewed commit |
| Cc: | Focuses: |
Description
The following error was triggered via wp-themes.com today:
E_ERROR: Uncaught Error: Call to undefined function switch_to_blog() in wp-includes/class-wp-user-query.php:1077
This seems to stem from #40613 [55657] (cc @spacedmonkey).
The theme code that triggered it was this:
https://themes.trac.wordpress.org/browser/sequel/1.0.4/functions.php?marks=185-190#L176
$contributor_ids = get_users( array( 'fields' => 'ID', 'orderby' => 'post_count', 'order' => 'DESC', 'who' => 'authors', ) );
Note that no blog_id is specified, as a result it's prefilled with get_current_blog_id() which return 1 on single-site.
https://github.com/WordPress/wordpress-develop/blob/trunk/src/wp-includes/class-wp-user-query.php#L94
The $blog_id is then initialised to 0 in several places to assume no-blog or a not-blog-specific request, such as this but then it hits the problematic code here
The actual issue is this:
if ( ( $args['has_published_posts'] && $blog_id ) || in_array( 'post_count', $ordersby, true ) ) {
It enters a branch that tries to switch, as ( $blog_id = 0 ) != get_current_blog_id().
Setting to 6.3.1 for review, as this seems like a regression.
Change History (10)
This ticket was mentioned in PR #4977 on WordPress/wordpress-develop by @dd32.
3 years ago
#1
- Keywords has-patch has-unit-tests added
#2
@
3 years ago
I was slightly wrong in my initial understanding of this issue.
This is specific to single-site with post_count and using the deprecated who=authors.
The problematic blog_id = 0 is mostly triggered from here it seems:
https://github.com/WordPress/wordpress-develop/blob/f7fbd0c25f6824f7259dbca47b38c6569901baf5/src/wp-includes/class-wp-user-query.php#L415-L417
See the PR for unit tests and patch.
#3
@
3 years ago
I can confirm this bug. Had a customer website PHP fatal when upgrading to WordPress core 6.3. Downgrading to 6.2.2 is a workaround for the error. For my testing I ran the following in wp shell
get_users( [ 'orderby' => 'post_count', 'order' => 'DESC', 'who' => 'authors' ] );
With core v6.3 the 'who' => 'authors' causes the fatal error. Works fine on version 6.2.2.
#4
@
3 years ago
Hi, confirmed with this in a plugin:
get_users( array( 'orderby' => 'post_count', 'order' => 'DESC', 'number' => '1000', 'fields' => array( 'ID' ), 'has_published_posts' => true, 'who' => 'authors' ) );
@SergeyBiryukov commented on PR #4977:
3 years ago
#6
Thanks for the PR! Merged in r56381.
#7
@
3 years ago
- Keywords fixed-major added
- Resolution fixed
- Status closed → reopened
Reopening for backporting to the 6.3 branch.
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)
Trac ticket: https://core.trac.wordpress.org/ticket/59011