diff --git a/src/wp-includes/class-wp-user-query.php b/src/wp-includes/class-wp-user-query.php index 02cae108c8..1fdbd03c37 100644 --- a/src/wp-includes/class-wp-user-query.php +++ b/src/wp-includes/class-wp-user-query.php @@ -255,13 +255,6 @@ class WP_User_Query { $blog_id = absint( $qv['blog_id'] ); } - if ( isset( $qv['who'] ) && 'authors' == $qv['who'] && $blog_id ) { - $qv['meta_key'] = $wpdb->get_blog_prefix( $blog_id ) . 'user_level'; - $qv['meta_value'] = 0; - $qv['meta_compare'] = '!='; - $qv['blog_id'] = $blog_id = 0; // Prevent extra meta query - } - if ( $qv['has_published_posts'] && $blog_id ) { if ( true === $qv['has_published_posts'] ) { $post_types = get_post_types( array( 'public' => true ) ); @@ -281,6 +274,29 @@ class WP_User_Query { $this->meta_query = new WP_Meta_Query(); $this->meta_query->parse_query_vars( $qv ); + if ( isset( $qv['who'] ) && 'authors' == $qv['who'] && $blog_id ) { + $who_query = array( + 'key' => $wpdb->get_blog_prefix( $blog_id ) . 'user_level', + 'value' => 0, + 'compare' => '!=', + ); + + // Prevent extra meta query. + $qv['blog_id'] = $blog_id = 0; + + if ( empty( $this->meta_query->queries ) ) { + $this->meta_query->queries = array( $who_query ); + } else { + // Append the cap query to the original queries and reparse the query. + $this->meta_query->queries = array( + 'relation' => 'AND', + array( $this->meta_query->queries, $who_query ), + ); + } + + $this->meta_query->parse_query_vars( $this->meta_query->queries ); + } + $roles = array(); if ( isset( $qv['role'] ) ) { if ( is_array( $qv['role'] ) ) { diff --git a/tests/phpunit/tests/user/query.php b/tests/phpunit/tests/user/query.php index 1fe2652f3c..e8b8a9ccf5 100644 --- a/tests/phpunit/tests/user/query.php +++ b/tests/phpunit/tests/user/query.php @@ -702,6 +702,37 @@ class Tests_User_Query extends WP_UnitTestCase { $this->assertNotContains( self::$author_ids[2], $found ); } + /** + * @ticket 36724 + */ + public function test_who_authors_should_work_alongside_meta_params() { + if ( ! is_multisite() ) { + $this->markTestSkipped( __METHOD__ . ' requires multisite.' ); + } + + $b = self::factory()->blog->create(); + + add_user_to_blog( $b, self::$author_ids[0], 'subscriber' ); + add_user_to_blog( $b, self::$author_ids[1], 'author' ); + add_user_to_blog( $b, self::$author_ids[2], 'editor' ); + + add_user_meta( self::$author_ids[1], 'foo', 'bar' ); + add_user_meta( self::$author_ids[2], 'foo', 'baz' ); + + $q = new WP_User_Query( array( + 'who' => 'authors', + 'blog_id' => $b, + 'meta_key' => 'foo', + 'meta_value' => 'bar', + ) ); + + $found = wp_list_pluck( $q->get_results(), 'ID' ); + + $this->assertNotContains( self::$author_ids[0], $found ); + $this->assertContains( self::$author_ids[1], $found ); + $this->assertNotContains( self::$author_ids[2], $found ); + } + /** * @ticket 32250 */