Index: src/wp-includes/wp-db.php
===================================================================
--- src/wp-includes/wp-db.php	(revision 27045)
+++ src/wp-includes/wp-db.php	(working copy)
@@ -510,6 +510,17 @@
 	public $is_mysql = null;
 
 	/**
+	 *  A list of MySQL's SQL modes to disallow when changing the SQL mode.
+	 *
+	 * @since 3.9.0
+	 * @see wpdb::set_sql_mode()
+	 * @access protected
+	 * @var array
+	 */
+	protected $incompatible_modes = array( 'NO_ZERO_DATE', 'ONLY_FULL_GROUP_BY',
+		'STRICT_TRANS_TABLES', 'STRICT_ALL_TABLES', 'TRADITIONAL' );
+
+	/**
 	 * Connects to the database server and selects a database
 	 *
 	 * PHP5 style constructor for compatibility with PHP5. Does
@@ -649,6 +660,46 @@
 	}
 
 	/**
+	 * Changes the current SQL mode, and ensures the SQL mode is
+	 * WordPress compatible. If no modes are passed, it will ensure
+	 * the current MySQL server modes are compatible.
+	 *
+	 * @since 3.9.0
+	 *
+	 * @param array $modes Optional. A list of SQL modes to set.
+	 */
+	function set_sql_mode( $modes = array() ) {
+		if ( empty( $modes ) ) {
+			$res = mysql_query( 'SELECT @@SESSION.sql_mode;', $this->dbh );
+			if ( empty( $res ) ) {
+				return;
+			}
+	
+			$modes_str = mysql_result( $res, 0 );
+
+			if ( empty( $modes_str ) ) {
+				return;
+			}
+	
+			$modes = explode( ',', $modes_str );
+		}
+		
+		$modes = array_change_key_case( $modes, CASE_UPPER );
+
+		$incompatible_modes = (array)apply_filters( 'incompatible_sql_modes', $this->incompatible_modes );
+
+		foreach( $modes as $i => $mode ) {
+			if ( in_array( $mode, $incompatible_modes ) ) {
+				unset( $modes[ $i ] );
+			}
+		}
+
+		$modes_str = implode( ',', $modes );
+
+		mysql_query( "SET SESSION sql_mode='$modes_str';", $this->dbh );
+	}
+
+	/**
 	 * Sets the table prefix for the WordPress tables.
 	 *
 	 * @since 2.5.0
@@ -1170,6 +1221,8 @@
 
 		$this->set_charset( $this->dbh );
 
+		$this->set_sql_mode();
+
 		$this->ready = true;
 
 		$this->select( $this->dbname, $this->dbh );
Index: tests/phpunit/tests/db.php
===================================================================
--- tests/phpunit/tests/db.php	(revision 27045)
+++ tests/phpunit/tests/db.php	(working copy)
@@ -126,4 +126,71 @@
 		$sql = $wpdb->prepare( "UPDATE test_table SET string_column = '%%f is a float, %%d is an int %d, %%s is a string', field = %s", 3, '4' );
 		$this->assertEquals( "UPDATE test_table SET string_column = '%f is a float, %d is an int 3, %s is a string', field = '4'", $sql );
 	}
+
+	/**
+	 * Test that SQL modes are set correctly
+	 * @ticket 26847
+	 */
+	public function test_set_sql_mode() {
+		global $wpdb;
+
+		$current_modes = $wpdb->get_var( 'SELECT @@SESSION.sql_mode;' );
+
+		$new_modes = array( 'IGNORE_SPACE', 'NO_AUTO_CREATE_USER' );
+		$wpdb->set_sql_mode( $new_modes );
+		$check_new_modes = $wpdb->get_var( 'SELECT @@SESSION.sql_mode;' );
+		$this->assertEquals( implode( ',', $new_modes ), $check_new_modes );
+
+		$wpdb->set_sql_mode( explode( ',', $current_modes ) );
+	}
+
+	/**
+	 * Test that incompatible SQL modes are blocked
+	 * @ticket 26847
+	 */
+	public function test_set_incompatible_sql_mode() {
+		global $wpdb;
+
+		$current_modes = $wpdb->get_var( 'SELECT @@SESSION.sql_mode;' );
+
+		$new_modes = array( 'IGNORE_SPACE', 'NO_ZERO_DATE', 'NO_AUTO_CREATE_USER' );
+		$wpdb->set_sql_mode( $new_modes );
+		$check_new_modes = $wpdb->get_var( 'SELECT @@SESSION.sql_mode;' );
+		$this->assertFalse( in_array( 'NO_ZERO_DATE', explode( ',', $check_new_modes ) ) );
+
+		$wpdb->set_sql_mode( explode( ',', $current_modes ) );
+	}
+
+	/**
+	 * Test that incompatible SQL modes can be changed
+	 * @ticket 26847
+	 */
+	public function test_set_allowed_incompatible_sql_mode() {
+		global $wpdb;
+
+		$current_modes = $wpdb->get_var( 'SELECT @@SESSION.sql_mode;' );
+
+		$new_modes = array( 'IGNORE_SPACE', 'NO_ZERO_DATE', 'NO_AUTO_CREATE_USER' );
+
+		add_filter( 'incompatible_sql_modes', array( $this, 'filter_allowed_incompatible_sql_mode' ), 1, 1 );
+		$wpdb->set_sql_mode( $new_modes );
+		remove_filter( 'incompatible_sql_modes', array( $this, 'filter_allowed_incompatible_sql_mode' ), 1 );
+
+		$check_new_modes = $wpdb->get_var( 'SELECT @@SESSION.sql_mode;' );
+		$this->assertTrue( in_array( 'NO_ZERO_DATE', explode( ',', $check_new_modes ) ) );
+
+		$wpdb->set_sql_mode( explode( ',', $current_modes ) );
+	}
+
+	public function filter_allowed_incompatible_sql_mode( $modes ) {
+		$pos = array_search( 'NO_ZERO_DATE', $modes );
+		$this->assertGreaterThanOrEqual( 0, $pos );
+
+		if ( FALSE === $pos ) {
+			return $modes;
+		}
+
+		unset( $modes[ $pos ] );
+		return $modes;
+	}
 }
