Index: src/wp-includes/class-wp-query.php
===================================================================
--- src/wp-includes/class-wp-query.php	(revision 49560)
+++ src/wp-includes/class-wp-query.php	(working copy)
@@ -915,7 +915,12 @@
 			}
 
 			if ( '' !== $qv['author_name'] ) {
-				$this->is_author = true;
+				if ( get_user_by( 'slug', $qv['author_name'] ) ) {
+					$this->is_author = true;
+				} else {
+					$qv['error'] = '404';
+					$qv['author'] = $qv['author_name'];
+				}
 			}
 
 			if ( ! empty( $qv['post_type'] ) && ! is_array( $qv['post_type'] ) ) {
Index: tests/phpunit/tests/query.php
===================================================================
--- tests/phpunit/tests/query.php	(revision 49560)
+++ tests/phpunit/tests/query.php	(working copy)
@@ -696,4 +696,26 @@
 		$this->assertSame( 'tax1', get_query_var( 'taxonomy' ) );
 		$this->assertSame( 'term1', get_query_var( 'term' ) );
 	}
+
+	/**
+	 * @ticket 51794
+	 */
+	public function test_nonexistent_author_query_should_not_return_results() {
+		$p1 = self::factory()->post->create( array( 'post_author' => 0 ) );
+
+		$url = add_query_arg(
+			array(
+				'author_name' => 'thisuserdoesnotexist',
+			),
+			'/'
+		);
+
+		$this->go_to( $url );
+
+		$matching_posts = wp_list_pluck( $GLOBALS['wp_query']->posts, 'ID' );
+
+		$this->assertTrue( $GLOBALS['wp_query']->is_404() );
+		$this->assertEmpty( $matching_posts );
+	}
+
 }
