diff --git src/wp-admin/includes/media.php src/wp-admin/includes/media.php
index 8a356dd..b005065 100644
--- src/wp-admin/includes/media.php
+++ src/wp-admin/includes/media.php
@@ -419,7 +419,7 @@ function media_handle_sideload( $file_array, $post_id, $desc = null, $post_data
 	$overrides = array( 'test_form' => false );
 
 	$time = current_time( 'mysql' );
-	if ( $post = get_post( $post_id ) ) {
+	if ( $post_id && $post = get_post( $post_id ) ) {
 		if ( substr( $post->post_date, 0, 4 ) > 0 ) {
 			$time = $post->post_date;
 		}
diff --git tests/phpunit/tests/media/media-handle-sideload.php tests/phpunit/tests/media/media-handle-sideload.php
new file mode 100644
index 0000000..9315eb9
--- /dev/null
+++ tests/phpunit/tests/media/media-handle-sideload.php
@@ -0,0 +1,124 @@
+<?php
+/**
+ * Tests for media side-loaded files.
+ *
+ * @package WordPress
+ */
+
+/**
+ * Tests for media side-loaded files.
+ *
+ * @group media
+ */
+class Tests_Media_MediaHandleSideload extends WP_UnitTestCase {
+
+	/**
+	 * Original file path.
+	 *
+	 * @var string
+	 */
+	protected static $orig_file;
+
+	/**
+	 * Test file path.
+	 *
+	 * @var string
+	 */
+	protected static $test_file;
+
+	/**
+	 * Setup before class.
+	 */
+	public static function setUpBeforeClass() {
+		parent::setUpBeforeClass();
+
+		self::$orig_file = DIR_TESTDATA . '/images/test-image.jpg';
+		self::$test_file = get_temp_dir() . 'test-image-media-sideload.jpg';
+
+		if ( ! file_exists( self::$test_file ) ) {
+			copy( self::$orig_file, self::$test_file );
+		}
+	}
+
+	/**
+	 * Tear down after class.
+	 */
+	public static function tearDownAfterClass() {
+		if ( file_exists( self::$test_file ) ) {
+			unlink( self::$test_file );
+		}
+		parent::tearDownAfterClass();
+	}
+
+	/**
+	 * Testing media_handle_sideload() with an existing post ID.
+	 *
+	 * @dataProvider data_post_id_provider
+	 *
+	 * @param mixed  $post_id            The post id to test for.
+	 * @param string $expected_directory Expected created directory.
+	 */
+	public function test_with_existing_post_id( $post_id, $expected_directory ) {
+
+		// The media_handle_sideload() deletes the tmp file.
+		if ( ! file_exists( self::$test_file ) ) {
+			copy( self::$orig_file, self::$test_file );
+		}
+
+		$file_array = array(
+			'name'     => basename( self::$test_file ),
+			'tmp_name' => self::$test_file,
+		);
+
+		// Handles the sideload.
+		$attachment_id = media_handle_sideload( $file_array, $post_id );
+
+		// Checks if the expected directory is in the file path.
+		$actual = strpos( get_attached_file( $attachment_id ), $expected_directory );
+
+		// Cleanup.
+		if ( ! is_wp_error( $attachment_id ) ) {
+			wp_delete_attachment( $attachment_id );
+		}
+
+		$this->assertNotFalse( $actual );
+	}
+
+	/**
+	 * Data provider for test_with_existing_post_id().
+	 *
+	 * @return array {
+	 *     @type array {
+	 *         @mixed mixed $post_id  The post id to test for.
+	 *         @string      $expected Expected created directory.
+	 *     }
+	 * }
+	 */
+	public function data_post_id_provider() {
+
+		$default = current_time( '/Y/m/' );
+
+		return array(
+			array(
+				'post_id'  => null,
+				'expected' => $default,
+			),
+			array(
+				'post_id'  => 0,
+				'expected' => $default,
+			),
+			array(
+				'post_id'  => false,
+				'expected' => $default,
+			),
+			array(
+				'post_id'  => self::factory()->post->create( array( 'post_date' => '0000-00-00' ) ),
+				'expected' => $default,
+			),
+			array(
+				'post_id'  => self::factory()->post->create( array( 'post_date' => '2018-01-01' ) ),
+				'expected' => '/2018/01',
+			),
+		);
+	}
+}
